From 9860438a93cece891ecdd502b7100c0d258bc3f3 Mon Sep 17 00:00:00 2001 From: Alex Thomson Date: Wed, 12 Sep 2018 11:09:01 +1200 Subject: [PATCH] Prevent movement outside of the world border --- build.gradle | 2 +- .../lxgaming/sledgehammer/Sledgehammer.java | 1 + .../sledgehammer/SledgehammerPlugin.java | 2 + .../category/IntegrationCategory.java | 7 +++ .../SpongeIntegration_Border.java | 54 +++++++++++++++++++ .../lxgaming/sledgehammer/util/Reference.java | 2 +- 6 files changed, 66 insertions(+), 2 deletions(-) create mode 100644 src/main/java/io/github/lxgaming/sledgehammer/integrations/SpongeIntegration_Border.java diff --git a/build.gradle b/build.gradle index 613ac04..ed4ca37 100644 --- a/build.gradle +++ b/build.gradle @@ -27,7 +27,7 @@ targetCompatibility = 1.8 group = "io.github.lxgaming" archivesBaseName = "Sledgehammer" -version = "1.12.2-1.2.7" +version = "1.12.2-1.2.8" minecraft { version = "1.12.2-14.23.4.2705" diff --git a/src/main/java/io/github/lxgaming/sledgehammer/Sledgehammer.java b/src/main/java/io/github/lxgaming/sledgehammer/Sledgehammer.java index 856a696..d1fea74 100644 --- a/src/main/java/io/github/lxgaming/sledgehammer/Sledgehammer.java +++ b/src/main/java/io/github/lxgaming/sledgehammer/Sledgehammer.java @@ -61,6 +61,7 @@ private void registerMappings() { getIntegrationMappings().put("io.github.lxgaming.sledgehammer.integrations.ForgeIntegration", IntegrationCategory::isForge); getIntegrationMappings().put("io.github.lxgaming.sledgehammer.integrations.MistIntegration", IntegrationCategory::isMist); getIntegrationMappings().put("io.github.lxgaming.sledgehammer.integrations.PrimalIntegration", IntegrationCategory::isPrimal); + getIntegrationMappings().put("io.github.lxgaming.sledgehammer.integrations.SpongeIntegration_Border", IntegrationCategory::isSpongeBorder); getIntegrationMappings().put("io.github.lxgaming.sledgehammer.integrations.SpongeIntegration_Death", IntegrationCategory::isSpongeDeath); getIntegrationMappings().put("io.github.lxgaming.sledgehammer.integrations.SpongeIntegration_Phase", IntegrationCategory::isSpongePhase); diff --git a/src/main/java/io/github/lxgaming/sledgehammer/SledgehammerPlugin.java b/src/main/java/io/github/lxgaming/sledgehammer/SledgehammerPlugin.java index 231c9cb..b49ff45 100644 --- a/src/main/java/io/github/lxgaming/sledgehammer/SledgehammerPlugin.java +++ b/src/main/java/io/github/lxgaming/sledgehammer/SledgehammerPlugin.java @@ -21,6 +21,7 @@ import io.github.lxgaming.sledgehammer.integrations.ForgeIntegration; import io.github.lxgaming.sledgehammer.integrations.MistIntegration; import io.github.lxgaming.sledgehammer.integrations.PrimalIntegration; +import io.github.lxgaming.sledgehammer.integrations.SpongeIntegration_Border; import io.github.lxgaming.sledgehammer.integrations.SpongeIntegration_Death; import io.github.lxgaming.sledgehammer.integrations.SpongeIntegration_Phase; import io.github.lxgaming.sledgehammer.managers.CommandManager; @@ -59,6 +60,7 @@ public void onGameInitialization(GameInitializationEvent event) { IntegrationManager.registerIntegration(ForgeIntegration.class); IntegrationManager.registerIntegration(MistIntegration.class); IntegrationManager.registerIntegration(PrimalIntegration.class); + IntegrationManager.registerIntegration(SpongeIntegration_Border.class); IntegrationManager.registerIntegration(SpongeIntegration_Death.class); IntegrationManager.registerIntegration(SpongeIntegration_Phase.class); } diff --git a/src/main/java/io/github/lxgaming/sledgehammer/configuration/category/IntegrationCategory.java b/src/main/java/io/github/lxgaming/sledgehammer/configuration/category/IntegrationCategory.java index b7115a2..f9934a6 100644 --- a/src/main/java/io/github/lxgaming/sledgehammer/configuration/category/IntegrationCategory.java +++ b/src/main/java/io/github/lxgaming/sledgehammer/configuration/category/IntegrationCategory.java @@ -31,6 +31,9 @@ public class IntegrationCategory { @Setting(value = "primal", comment = "Fixes https://github.com/An-Sar/PrimalCore/issues/210 (Fixed in 0.6.56 according to Dev)") private boolean primal = false; + @Setting(value = "sponge-border", comment = "Prevent movement outside of the world border") + private boolean spongeBorder = false; + @Setting(value = "sponge-death", comment = "Prevent sending blank death messages") private boolean spongeDeath = false; @@ -49,6 +52,10 @@ public boolean isPrimal() { return primal; } + public boolean isSpongeBorder() { + return spongeBorder; + } + public boolean isSpongeDeath() { return spongeDeath; } diff --git a/src/main/java/io/github/lxgaming/sledgehammer/integrations/SpongeIntegration_Border.java b/src/main/java/io/github/lxgaming/sledgehammer/integrations/SpongeIntegration_Border.java new file mode 100644 index 0000000..915d058 --- /dev/null +++ b/src/main/java/io/github/lxgaming/sledgehammer/integrations/SpongeIntegration_Border.java @@ -0,0 +1,54 @@ +/* + * Copyright 2018 Alex Thomson + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package io.github.lxgaming.sledgehammer.integrations; + +import com.flowpowered.math.vector.Vector3d; +import io.github.lxgaming.sledgehammer.Sledgehammer; +import org.spongepowered.api.Sponge; +import org.spongepowered.api.entity.living.player.Player; +import org.spongepowered.api.event.Listener; +import org.spongepowered.api.event.Order; +import org.spongepowered.api.event.entity.MoveEntityEvent; +import org.spongepowered.api.event.filter.cause.Root; +import org.spongepowered.api.world.Location; +import org.spongepowered.api.world.World; + +public class SpongeIntegration_Border extends AbstractIntegration { + + public SpongeIntegration_Border() { + addDependency("sponge"); + } + + @Override + public boolean prepareIntegration() { + Sponge.getEventManager().registerListeners(Sledgehammer.getInstance().getPluginContainer(), this); + return true; + } + + @Listener(order = Order.LAST) + public void onMoveEntity(MoveEntityEvent event, @Root Player player) { + // https://github.com/NucleusPowered/Nucleus/blob/c55d92191741214b09a6952ca853723c27f640d0/src/main/java/io/github/nucleuspowered/nucleus/Util.java#L393 + Location location = event.getToTransform().getLocation(); + World world = location.getExtent(); + long radius = (long) Math.floor(world.getWorldBorder().getDiameter() / 2.0) + 1L; + Vector3d displacement = location.getPosition().sub(world.getWorldBorder().getCenter()).abs(); + if (displacement.getX() > radius || displacement.getZ() > radius) { + event.setCancelled(true); + Sledgehammer.getInstance().debugMessage("Move denied for {} ({})", player.getName(), player.getUniqueId()); + } + } +} \ No newline at end of file diff --git a/src/main/java/io/github/lxgaming/sledgehammer/util/Reference.java b/src/main/java/io/github/lxgaming/sledgehammer/util/Reference.java index 5adbaec..430a9fb 100644 --- a/src/main/java/io/github/lxgaming/sledgehammer/util/Reference.java +++ b/src/main/java/io/github/lxgaming/sledgehammer/util/Reference.java @@ -20,7 +20,7 @@ public class Reference { public static final String PLUGIN_ID = "sledgehammer"; public static final String PLUGIN_NAME = "Sledgehammer"; - public static final String PLUGIN_VERSION = "1.12.2-1.2.7"; + public static final String PLUGIN_VERSION = "1.12.2-1.2.8"; public static final String DESCRIPTION = "Smashes the stupid out of the server."; public static final String AUTHORS = "LX_Gaming"; public static final String SOURCE = "https://github.com/LXGaming/Sledgehammer/";