From ad31a904555c0226517c2fdbd8ade950beb8d7cb Mon Sep 17 00:00:00 2001 From: DmitriyMX Date: Sat, 8 Sep 2018 14:54:29 +0300 Subject: [PATCH] =?UTF-8?q?=D0=B4=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5?= =?UTF-8?q?=D0=BD=20ImmutableEntityLocation?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/mc/core/ImmutableEntityLocation.java | 57 +++++++++++++++++++ .../eventbus/events/CS_PlayerMoveEvent.java | 11 ++-- .../mc/core/TestImmutableEntityLocation.java | 35 ++++++++++++ 3 files changed, 99 insertions(+), 4 deletions(-) create mode 100644 core/src/main/java/mc/core/ImmutableEntityLocation.java create mode 100644 core/src/test/java/mc/core/TestImmutableEntityLocation.java diff --git a/core/src/main/java/mc/core/ImmutableEntityLocation.java b/core/src/main/java/mc/core/ImmutableEntityLocation.java new file mode 100644 index 0000000..f71c1e3 --- /dev/null +++ b/core/src/main/java/mc/core/ImmutableEntityLocation.java @@ -0,0 +1,57 @@ +package mc.core; + +public class ImmutableEntityLocation extends EntityLocation { + public ImmutableEntityLocation(double x, double y, double z, float yaw, float pitch) { + super(x, y, z, yaw, pitch); + } + + public ImmutableEntityLocation(EntityLocation location) { + this( + location.getX(), + location.getY(), + location.getZ(), + location.getYaw(), + location.getPitch() + ); + } + + @Override + public void setX(double x) { + throw new UnsupportedOperationException(); + } + + @Override + public void setY(double y) { + throw new UnsupportedOperationException(); + } + + @Override + public void setZ(double z) { + throw new UnsupportedOperationException(); + } + + @Override + public void setYaw(float yaw) { + throw new UnsupportedOperationException(); + } + + @Override + public void setPitch(float pitch) { + throw new UnsupportedOperationException(); + } + + @Override + public void set(EntityLocation location) { + throw new UnsupportedOperationException(); + } + + @Override + public void setXYZ(double x, double y, double z) { + throw new UnsupportedOperationException(); + } + + @Override + public void setYawPitch(float yaw, float pitch) { + throw new UnsupportedOperationException(); + } +} diff --git a/core/src/main/java/mc/core/eventbus/events/CS_PlayerMoveEvent.java b/core/src/main/java/mc/core/eventbus/events/CS_PlayerMoveEvent.java index ae60200..4d759ab 100644 --- a/core/src/main/java/mc/core/eventbus/events/CS_PlayerMoveEvent.java +++ b/core/src/main/java/mc/core/eventbus/events/CS_PlayerMoveEvent.java @@ -5,20 +5,23 @@ package mc.core.eventbus.events; import lombok.Getter; -import lombok.RequiredArgsConstructor; import lombok.Setter; import mc.core.EntityLocation; +import mc.core.ImmutableEntityLocation; import mc.core.eventbus.EventBase; import mc.core.player.Player; -@RequiredArgsConstructor @Getter public class CS_PlayerMoveEvent extends EventBase { private final Player player; - private final EntityLocation oldLocation; // TODO сомнительное решение - // вообще нужно будет создать реализацию "иммутабл локейшен" для подобных ситуаций + private final ImmutableEntityLocation oldLocation; @Setter private EntityLocation newLocation; @Setter private boolean recalcChunk = false; + + public CS_PlayerMoveEvent(Player player, EntityLocation oldLocation) { + this.player = player; + this.oldLocation = new ImmutableEntityLocation(oldLocation); + } } diff --git a/core/src/test/java/mc/core/TestImmutableEntityLocation.java b/core/src/test/java/mc/core/TestImmutableEntityLocation.java new file mode 100644 index 0000000..2f6a958 --- /dev/null +++ b/core/src/test/java/mc/core/TestImmutableEntityLocation.java @@ -0,0 +1,35 @@ +package mc.core; + +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; + +import static org.junit.Assert.assertEquals; + +public class TestImmutableEntityLocation { + @Rule + public ExpectedException thrown = ExpectedException.none(); + + @Test + public void testSetValue() { + EntityLocation location = new ImmutableEntityLocation(1d, 2d, 3d, 4f, 5f); + + thrown.expect(UnsupportedOperationException.class); + location.setX(1); + location.setY(1); + location.setZ(1); + location.setYaw(1); + location.setPitch(1); + location.setXYZ(1, 2, 3); + location.setYawPitch(1, 2); + location.set(EntityLocation.ZERO()); + } + + @Test + public void testClone() { + EntityLocation locOrig = new ImmutableEntityLocation(1d, 2d, 3d, 4f, 5f); + EntityLocation locClone = locOrig.clone(); + + assertEquals(locOrig, locClone); + } +} \ No newline at end of file