Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Start implementing advanced rotation #196

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* SPDX-License-Identifier: AGPL-3.0-only
*
* Copyright (C) 2021 EldoriaRPG Team and Contributor
*/

package de.eldoria.schematicbrush.brush.config.advancedrotation;

import de.eldoria.eldoutilities.commands.command.util.CommandAssertions;
import de.eldoria.eldoutilities.commands.exceptions.CommandException;
import de.eldoria.schematicbrush.brush.config.rotation.Rotation;
import de.eldoria.schematicbrush.brush.config.util.Shiftable;

import java.util.function.Supplier;

/**
* Represents a rotation.
*/
@SuppressWarnings("InterfaceMayBeAnnotatedFunctional")
public interface AdvancedRotation extends Shiftable<AdvancedRotation> {

/**
* Rotation represented as positive int value.
*
* @return rotation as positive integer
*/
Supplier<Rotation> x();
/**
* Rotation represented as positive int value.
*
* @return rotation as positive integer
*/
Supplier<Rotation> y();
/**
* Rotation represented as positive int value.
*
* @return rotation as positive integer
*/
Supplier<Rotation> z();

@Override
default void value(AdvancedRotation value) {
}

@Override
default AdvancedRotation value() {
return this;
}

@Override
default AdvancedRotation valueProvider() {
return this;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/*
* SPDX-License-Identifier: AGPL-3.0-only
*
* Copyright (C) 2021 EldoriaRPG Team and Contributor
*/

package de.eldoria.schematicbrush.brush.config.advancedrotation;

import de.eldoria.eldoutilities.serialization.SerializationUtil;
import de.eldoria.schematicbrush.brush.PasteMutation;
import de.eldoria.schematicbrush.brush.config.provider.Mutator;
import de.eldoria.schematicbrush.brush.config.rotation.Rotation;
import org.jetbrains.annotations.NotNull;

import java.util.List;
import java.util.Map;
import java.util.Objects;

public abstract class AAdvancedRotation implements Mutator<Rotation> {
protected AdvancedRotation rotation = null;

public AAdvancedRotation(AdvancedRotation rotation) {
this.rotation = rotation;
}

public AAdvancedRotation(Map<String, Object> objectMap) {
var map = SerializationUtil.mapOf(objectMap);
rotation = AdvancedRotation.valueOf(map.getValue("value"));
}

public AAdvancedRotation() {
}

public static AAdvancedRotation fixed(Rotation x, Rotation y, Rotation z) {
return new AdvancedRotationFixed(x, y, z);
}

public static AAdvancedRotation list(List<RotationSupplier> rotations) {
return new AdvancedRotationList(rotations);
}

public static AAdvancedRotation random(RandomRotationSupplier x, RandomRotationSupplier y, RandomRotationSupplier z) {
return new AdvancedRotationRandom(x, y, z);
}

@NotNull
public Map<String, Object> serialize() {
return SerializationUtil.newBuilder()
.add("value", rotation.degree())
.build();
}

@Override
public void value(Rotation value) {
rotation = value;
}

@Override
public Rotation value() {
return rotation;
}

@Override
public void invoke(PasteMutation mutation) {
if (rotation.value().degree() != 0) {
mutation.transform(mutation.transform().rotateY(rotation.value().degree()));
}
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof AAdvancedRotation aAdvancedRotation)) return false;

return Objects.equals(rotation, aAdvancedRotation.rotation);
}

@Override
public int hashCode() {
return rotation != null ? rotation.hashCode() : 0;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* SPDX-License-Identifier: AGPL-3.0-only
*
* Copyright (C) 2021 EldoriaRPG Team and Contributor
*/

package de.eldoria.schematicbrush.brush.config.advancedrotation;

import de.eldoria.schematicbrush.brush.config.provider.Mutator;
import de.eldoria.schematicbrush.brush.config.rotation.Rotation;
import org.bukkit.configuration.serialization.SerializableAs;

import java.util.Map;

@SerializableAs("sbrRotationFixed")
public class AdvancedRotationFixed extends AAdvancedRotation {
public AdvancedRotationFixed(Rotation rotation, Rotation y, Rotation z) {
super(rotation);
}

public AdvancedRotationFixed(Map<String, Object> objectMap) {
super(objectMap);
}

@Override
public Rotation valueProvider() {
return rotation;
}

@Override
public String descriptor() {
return String.format("%s", rotation.degree());
}

@Override
public String name() {
return "Fixed";
}

@Override
public Mutator<Rotation> copy() {
return new AdvancedRotationFixed(rotation);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
/*
* SPDX-License-Identifier: AGPL-3.0-only
*
* Copyright (C) 2021 EldoriaRPG Team and Contributor
*/

package de.eldoria.schematicbrush.brush.config.advancedrotation;

import de.eldoria.eldoutilities.serialization.SerializationUtil;
import de.eldoria.schematicbrush.brush.config.provider.Mutator;
import de.eldoria.schematicbrush.brush.config.rotation.Rotation;
import org.bukkit.configuration.serialization.SerializableAs;
import org.jetbrains.annotations.NotNull;

import java.util.List;
import java.util.Map;
import java.util.concurrent.ThreadLocalRandom;
import java.util.stream.Collectors;

@SerializableAs("sbrRotationList")
public class AdvancedRotationList extends AAdvancedRotation {
private final List<Rotation> values;

public AdvancedRotationList(List<RotationSupplier> values) {
this.values = values;
}

public AdvancedRotationList(Map<String, Object> objectMap) {
var map = SerializationUtil.mapOf(objectMap);
List<Integer> values = map.getValue("values");
this.values = values.stream().map(Rotation::valueOf).collect(Collectors.toList());
}

@Override
public Rotation valueProvider() {
return values.get(ThreadLocalRandom.current().nextInt(values.size()));
}

@Override
public void shift() {
if (value() == null) {
value(values.get(ThreadLocalRandom.current().nextInt(values.size())));
}
var index = values.indexOf(value());
Rotation newValue;
if (index + 1 == values.size()) {
newValue = values.get(0);
} else {
newValue = values.get(index + 1);
}
value(newValue);
}

@Override
public boolean shiftable() {
return true;
}

@Override
@NotNull
public Map<String, Object> serialize() {
return SerializationUtil.newBuilder()
.add("values", values.stream().map(Rotation::degree).collect(Collectors.toList()))
.build();
}

@Override
public String descriptor() {
return values.stream().map(Rotation::degree).map(String::valueOf).collect(Collectors.joining(", "));
}

@Override
public String name() {
return "List";
}

@Override
public Mutator<Rotation> copy() {
return new AdvancedRotationList(values);
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof AdvancedRotationList rotationList)) return false;

return values.equals(rotationList.values);
}

@Override
public int hashCode() {
var result = super.hashCode();
result = 31 * result + values.hashCode();
return result;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* SPDX-License-Identifier: AGPL-3.0-only
*
* Copyright (C) 2021 EldoriaRPG Team and Contributor
*/

package de.eldoria.schematicbrush.brush.config.advancedrotation;

import de.eldoria.eldoutilities.serialization.SerializationUtil;
import de.eldoria.schematicbrush.brush.config.provider.Mutator;
import de.eldoria.schematicbrush.brush.config.rotation.Rotation;
import org.bukkit.configuration.serialization.SerializableAs;
import org.jetbrains.annotations.NotNull;

import java.util.Arrays;
import java.util.Map;

@SerializableAs("sbrRotationRandom")
public class AdvancedRotationRandom extends AdvancedRotationList {
public AdvancedRotationRandom(RandomRotationSupplier x, RandomRotationSupplier y, RandomRotationSupplier z) {
super(Arrays.asList(Rotation.ROT_ZERO, Rotation.ROT_RIGHT, Rotation.ROT_HALF, Rotation.ROT_LEFT));
}

public AdvancedRotationRandom(Map<String, Object> objectMap) {
this(x, y, z);
}

@Override
public boolean shiftable() {
return true;
}

@Override
@NotNull
public Map<String, Object> serialize() {
return SerializationUtil.newBuilder().build();
}

@Override
public String descriptor() {
return "";
}

@Override
public String name() {
return "Random";
}

@Override
public Mutator<Rotation> copy() {
return new AdvancedRotationRandom(x, y, z);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package de.eldoria.schematicbrush.brush.config.advancedrotation;

import de.eldoria.schematicbrush.brush.config.rotation.Rotation;

import java.util.List;
import java.util.concurrent.ThreadLocalRandom;

public class RandomRotationSupplier {
// TODO serialization
private final List<Rotation> rotations;

public RandomRotationSupplier(List<Rotation> rotations) {
this.rotations = rotations;
}

public Rotation rotation() {
return rotations.get(ThreadLocalRandom.current().nextInt(0, rotations.size()));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package de.eldoria.schematicbrush.brush.config.advancedrotation;

import de.eldoria.schematicbrush.brush.config.rotation.Rotation;

import java.util.concurrent.ThreadLocalRandom;

public class RotationSupplier {
// TODO serialization
private final Rotation x;
private final Rotation y;
private final Rotation z;

public RotationSupplier(Rotation x, Rotation y, Rotation z) {
this.x = x;
this.y = y;
this.z = z;
}

public Rotation x() {
return x;
}

public Rotation y() {
return y;
}

public Rotation z() {
return z;
}

public Rotation rotation() {
return rotations.get(ThreadLocalRandom.current().nextInt(0, rotations.size()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import de.eldoria.eldoutilities.serialization.SerializationUtil;
import de.eldoria.schematicbrush.brush.PasteMutation;
import de.eldoria.schematicbrush.brush.config.advancedrotation.AdvancedRotation;
import de.eldoria.schematicbrush.brush.config.provider.Mutator;
import org.jetbrains.annotations.NotNull;

Expand Down
Loading