From 87c877942729f4ee4d7b5092353663a66db8d346 Mon Sep 17 00:00:00 2001 From: Alex Thomson Date: Thu, 13 Dec 2018 01:02:44 +1300 Subject: [PATCH] Optimize EntityTeleport fix Now supports Minecarts Attempts to save entities by cancelling setPortal --- build.gradle | 16 +-- .../lxgaming/sledgehammer/Sledgehammer.java | 2 +- .../sledgehammer/SledgehammerPlugin.java | 2 - .../category/IntegrationCategory.java | 17 --- .../category/MessageCategory.java | 2 +- .../configuration/category/MixinCategory.java | 17 +++ .../SpongeIntegration_ItemTeleport.java | 100 ------------------ .../core/entity/MixinEntity_Teleport.java | 85 +++++++++++++++ .../lxgaming/sledgehammer/util/Reference.java | 2 +- .../lxgaming/sledgehammer/util/Toolbox.java | 11 ++ .../resources/mixins.sledgehammer.core.json | 1 + 11 files changed, 126 insertions(+), 129 deletions(-) delete mode 100644 src/main/java/io/github/lxgaming/sledgehammer/integrations/SpongeIntegration_ItemTeleport.java create mode 100644 src/main/java/io/github/lxgaming/sledgehammer/mixin/core/entity/MixinEntity_Teleport.java diff --git a/build.gradle b/build.gradle index 43a95cf..31e72b7 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.15" +version = "1.12.2-1.2.16" minecraft { version = "1.12.2-14.23.5.2768" @@ -63,15 +63,15 @@ repositories { } dependencies { - annotationProcessor("org.spongepowered:spongecommon:7.1.0:dev") - provided("org.spongepowered:spongecommon:7.1.0:dev") { + annotationProcessor("org.spongepowered:spongecommon:1.12.2-7.1.4:dev") + provided("org.spongepowered:spongecommon:1.12.2-7.1.4:dev") { exclude(module: "commons-io") exclude(module: "gson") exclude(module: "guava") exclude(module: "launchwrapper") } - provided("org.spongepowered:spongeforge:1.12.2-2705-7.1.0-BETA-3361:dev") - provided("org.spongepowered:spongevanilla:1.12.2-7.1.0-BETA-59:dev") + provided("org.spongepowered:spongeforge:1.12.2-2768-7.1.4:dev") + provided("org.spongepowered:spongevanilla:1.12.2-7.1.4:dev") provided("primalcore:PrimalCore:1.12.2:0.6.51") } @@ -87,9 +87,11 @@ jar { } from { - (configurations.compile - configurations.provided).collect { + (configurations.compile - configurations.provided).findAll({ + it.isDirectory() || it.name.endsWith(".jar") + }).collect({ it.isDirectory() ? it : zipTree(it) - } + }) } exclude("dummyThing") diff --git a/src/main/java/io/github/lxgaming/sledgehammer/Sledgehammer.java b/src/main/java/io/github/lxgaming/sledgehammer/Sledgehammer.java index 285d3ef..3196a31 100644 --- a/src/main/java/io/github/lxgaming/sledgehammer/Sledgehammer.java +++ b/src/main/java/io/github/lxgaming/sledgehammer/Sledgehammer.java @@ -63,13 +63,13 @@ private void registerMappings() { 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_ItemTeleport", IntegrationCategory::isSpongeItemTeleport); getIntegrationMappings().put("io.github.lxgaming.sledgehammer.integrations.SpongeIntegration_Phase", IntegrationCategory::isSpongePhase); // Mixin Core getMixinMappings().put("io.github.lxgaming.sledgehammer.mixin.core.advancements.MixinAdvancementManager", MixinCategory::isAdvancementStacktrace); getMixinMappings().put("io.github.lxgaming.sledgehammer.mixin.core.block.MixinBlockGrass", MixinCategory::isBlockGrass); getMixinMappings().put("io.github.lxgaming.sledgehammer.mixin.core.block.MixinBlockIce", MixinCategory::isBlockIce); + getMixinMappings().put("io.github.lxgaming.sledgehammer.mixin.core.entity.MixinEntity_Teleport", MixinCategory::isItemTeleport); getMixinMappings().put("io.github.lxgaming.sledgehammer.mixin.core.network.MixinNetHandlerPlayServer", MixinCategory::isInteractEvents); getMixinMappings().put("io.github.lxgaming.sledgehammer.mixin.core.network.MixinNetworkManager", MixinCategory::isFlushNetworkOnTick); getMixinMappings().put("io.github.lxgaming.sledgehammer.mixin.core.network.MixinNetworkSystem", MixinCategory::isNetworkSystem); diff --git a/src/main/java/io/github/lxgaming/sledgehammer/SledgehammerPlugin.java b/src/main/java/io/github/lxgaming/sledgehammer/SledgehammerPlugin.java index 3578d70..b49ff45 100644 --- a/src/main/java/io/github/lxgaming/sledgehammer/SledgehammerPlugin.java +++ b/src/main/java/io/github/lxgaming/sledgehammer/SledgehammerPlugin.java @@ -23,7 +23,6 @@ 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_ItemTeleport; import io.github.lxgaming.sledgehammer.integrations.SpongeIntegration_Phase; import io.github.lxgaming.sledgehammer.managers.CommandManager; import io.github.lxgaming.sledgehammer.managers.IntegrationManager; @@ -63,7 +62,6 @@ public void onGameInitialization(GameInitializationEvent event) { IntegrationManager.registerIntegration(PrimalIntegration.class); IntegrationManager.registerIntegration(SpongeIntegration_Border.class); IntegrationManager.registerIntegration(SpongeIntegration_Death.class); - IntegrationManager.registerIntegration(SpongeIntegration_ItemTeleport.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 b9cf8e9..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 @@ -16,12 +16,9 @@ package io.github.lxgaming.sledgehammer.configuration.category; -import io.github.lxgaming.sledgehammer.util.Toolbox; import ninja.leaping.configurate.objectmapping.Setting; import ninja.leaping.configurate.objectmapping.serialize.ConfigSerializable; -import java.util.List; - @ConfigSerializable public class IntegrationCategory { @@ -40,12 +37,6 @@ public class IntegrationCategory { @Setting(value = "sponge-death", comment = "Prevent sending blank death messages") private boolean spongeDeath = false; - @Setting(value = "sponge-item-teleport", comment = "Deletes any items that teleport across dimensions") - private boolean spongeItemTeleport = false; - - @Setting(value = "sponge-item-whitelist", comment = "Don't delete these items") - private List spongeItemWhitelist = Toolbox.newArrayList("draconicevolution:ender_energy_manipulator"); - @Setting(value = "sponge-phase", comment = "Fixes https://github.com/SpongePowered/SpongeForge/issues/2355") private boolean spongePhase = false; @@ -69,14 +60,6 @@ public boolean isSpongeDeath() { return spongeDeath; } - public boolean isSpongeItemTeleport() { - return spongeItemTeleport; - } - - public List getSpongeItemWhitelist() { - return spongeItemWhitelist; - } - public boolean isSpongePhase() { return spongePhase; } diff --git a/src/main/java/io/github/lxgaming/sledgehammer/configuration/category/MessageCategory.java b/src/main/java/io/github/lxgaming/sledgehammer/configuration/category/MessageCategory.java index 2b05214..b3fb251 100644 --- a/src/main/java/io/github/lxgaming/sledgehammer/configuration/category/MessageCategory.java +++ b/src/main/java/io/github/lxgaming/sledgehammer/configuration/category/MessageCategory.java @@ -26,7 +26,7 @@ public class MessageCategory { private String moveOutsideBorder = "&cCannot move outside of the world border"; @Setting(value = "item-teleport", comment = "Sent to the player when a thrown item gets deleted") - private String itemTeleport = "&f[ITEM] &cwas lost in time and space"; + private String itemTeleport = "&f[ID] &cwas lost in time and space"; public String getMoveOutsideBorder() { return moveOutsideBorder; diff --git a/src/main/java/io/github/lxgaming/sledgehammer/configuration/category/MixinCategory.java b/src/main/java/io/github/lxgaming/sledgehammer/configuration/category/MixinCategory.java index 93dd9ee..83f06c9 100644 --- a/src/main/java/io/github/lxgaming/sledgehammer/configuration/category/MixinCategory.java +++ b/src/main/java/io/github/lxgaming/sledgehammer/configuration/category/MixinCategory.java @@ -16,9 +16,12 @@ package io.github.lxgaming.sledgehammer.configuration.category; +import com.google.common.collect.Lists; import ninja.leaping.configurate.objectmapping.Setting; import ninja.leaping.configurate.objectmapping.serialize.ConfigSerializable; +import java.util.List; + @ConfigSerializable public class MixinCategory { @@ -52,6 +55,12 @@ public class MixinCategory { @Setting(value = "packet-spam", comment = "Cancels spammy packets") private boolean packetSpam = false; + @Setting(value = "item-teleport", comment = "Prevents or deletes any items that attempt to teleport across dimensions") + private boolean itemTeleport = false; + + @Setting(value = "item-teleport-whitelist", comment = "Don't prevent these items from teleporting") + private List itemTeleportWhitelist = Lists.newArrayList("draconicevolution:ender_energy_manipulator"); + @Setting(value = "traveling-merchant", comment = "Fixes https://github.com/Daveyx0/PrimitiveMobs/issues/59") private boolean travelingMerchant = false; @@ -95,6 +104,14 @@ public boolean isPacketSpam() { return packetSpam; } + public boolean isItemTeleport() { + return itemTeleport; + } + + public List getItemTeleportWhitelist() { + return itemTeleportWhitelist; + } + public boolean isTravelingMerchant() { return travelingMerchant; } diff --git a/src/main/java/io/github/lxgaming/sledgehammer/integrations/SpongeIntegration_ItemTeleport.java b/src/main/java/io/github/lxgaming/sledgehammer/integrations/SpongeIntegration_ItemTeleport.java deleted file mode 100644 index 6fd0102..0000000 --- a/src/main/java/io/github/lxgaming/sledgehammer/integrations/SpongeIntegration_ItemTeleport.java +++ /dev/null @@ -1,100 +0,0 @@ -/* - * 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 io.github.lxgaming.sledgehammer.Sledgehammer; -import io.github.lxgaming.sledgehammer.configuration.Config; -import io.github.lxgaming.sledgehammer.configuration.category.IntegrationCategory; -import io.github.lxgaming.sledgehammer.configuration.category.MessageCategory; -import io.github.lxgaming.sledgehammer.util.Toolbox; -import org.apache.commons.lang3.StringUtils; -import org.spongepowered.api.Sponge; -import org.spongepowered.api.block.BlockTypes; -import org.spongepowered.api.entity.Item; -import org.spongepowered.api.event.Listener; -import org.spongepowered.api.event.Order; -import org.spongepowered.api.event.block.CollideBlockEvent; -import org.spongepowered.api.event.entity.MoveEntityEvent; -import org.spongepowered.api.event.filter.cause.Root; -import org.spongepowered.api.text.Text; - -public class SpongeIntegration_ItemTeleport extends AbstractIntegration { - - public SpongeIntegration_ItemTeleport() { - addDependency("sponge"); - } - - @Override - public boolean prepareIntegration() { - Sponge.getEventManager().registerListeners(Sledgehammer.getInstance().getPluginContainer(), this); - return true; - } - - @Listener(order = Order.LAST) - public void onCollideBlock(CollideBlockEvent event, @Root Item item) { - if (event.getTargetBlock().getType() != BlockTypes.PORTAL && event.getTargetBlock().getType() != BlockTypes.END_PORTAL) { - return; - } - - if (item.isRemoved()) { - return; - } - - if (Sledgehammer.getInstance().getConfig() - .map(Config::getIntegrationCategory) - .map(IntegrationCategory::getSpongeItemWhitelist) - .map(list -> list.contains(item.getItemType().getId())).orElse(false)) { - return; - } - - item.remove(); - - Sledgehammer.getInstance().debugMessage("Item {} removed", item.getItemType().getId()); - Sledgehammer.getInstance().getConfig().map(Config::getMessageCategory).map(MessageCategory::getItemTeleport).filter(StringUtils::isNotBlank).ifPresent(message -> { - item.getCreator().flatMap(Sponge.getServer()::getPlayer).ifPresent(player -> { - player.sendMessage(Text.of(Toolbox.getTextPrefix(), Toolbox.convertColor(message.replace("[ITEM]", item.getItemType().getId())))); - }); - }); - } - - @Listener(order = Order.LAST) - public void onMoveEntity(MoveEntityEvent.Teleport event, @Root Item item) { - if (event.getFromTransform().getExtent() == event.getToTransform().getExtent()) { - return; - } - - if (item.isRemoved()) { - return; - } - - if (Sledgehammer.getInstance().getConfig() - .map(Config::getIntegrationCategory) - .map(IntegrationCategory::getSpongeItemWhitelist) - .map(list -> list.contains(item.getItemType().getId())).orElse(false)) { - return; - } - - item.remove(); - - Sledgehammer.getInstance().debugMessage("Item {} removed", item.getItemType().getId()); - Sledgehammer.getInstance().getConfig().map(Config::getMessageCategory).map(MessageCategory::getItemTeleport).filter(StringUtils::isNotBlank).ifPresent(message -> { - item.getCreator().flatMap(Sponge.getServer()::getPlayer).ifPresent(player -> { - player.sendMessage(Text.of(Toolbox.getTextPrefix(), Toolbox.convertColor(message.replace("[ITEM]", item.getItemType().getId())))); - }); - }); - } -} \ No newline at end of file diff --git a/src/main/java/io/github/lxgaming/sledgehammer/mixin/core/entity/MixinEntity_Teleport.java b/src/main/java/io/github/lxgaming/sledgehammer/mixin/core/entity/MixinEntity_Teleport.java new file mode 100644 index 0000000..5873340 --- /dev/null +++ b/src/main/java/io/github/lxgaming/sledgehammer/mixin/core/entity/MixinEntity_Teleport.java @@ -0,0 +1,85 @@ +/* + * 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.mixin.core.entity; + +import io.github.lxgaming.sledgehammer.Sledgehammer; +import io.github.lxgaming.sledgehammer.configuration.Config; +import io.github.lxgaming.sledgehammer.configuration.category.MessageCategory; +import io.github.lxgaming.sledgehammer.configuration.category.MixinCategory; +import io.github.lxgaming.sledgehammer.util.Toolbox; +import net.minecraft.entity.Entity; +import net.minecraftforge.common.util.ITeleporter; +import org.apache.commons.lang3.StringUtils; +import org.spongepowered.api.Sponge; +import org.spongepowered.api.entity.Item; +import org.spongepowered.api.entity.vehicle.minecart.Minecart; +import org.spongepowered.api.text.Text; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Inject; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; +import org.spongepowered.common.interfaces.entity.IMixinEntity; + +@Mixin(value = Entity.class, priority = 1337) +public abstract class MixinEntity_Teleport { + + @Inject(method = "setPortal", at = @At(value = "HEAD"), cancellable = true) + private void onSetPortal(CallbackInfo callbackInfo) { + if (shouldRemove((IMixinEntity) this)) { + callbackInfo.cancel(); + } + } + + @Inject(method = "changeDimension(I)Lnet/minecraft/entity/Entity;", at = @At(value = "HEAD")) + private void onChangeDimension(int dimension, CallbackInfoReturnable callbackInfoReturnable) { + IMixinEntity mixinEntity = (IMixinEntity) this; + if (shouldRemove(mixinEntity)) { + remove(mixinEntity); + } + } + + @Inject(method = "changeDimension(ILnet/minecraftforge/common/util/ITeleporter;)Lnet/minecraft/entity/Entity;", at = @At(value = "HEAD"), remap = false) + private void onChangeDimension(int dimension, ITeleporter teleporter, CallbackInfoReturnable callbackInfoReturnable) { + IMixinEntity mixinEntity = (IMixinEntity) this; + if (shouldRemove(mixinEntity)) { + remove(mixinEntity); + } + } + + private void remove(IMixinEntity mixinEntity) { + mixinEntity.remove(); + + Sledgehammer.getInstance().debugMessage("Entity {} removed", Toolbox.getRootType(mixinEntity).getId()); + Sledgehammer.getInstance().getConfig().map(Config::getMessageCategory).map(MessageCategory::getItemTeleport).filter(StringUtils::isNotBlank).ifPresent(message -> { + mixinEntity.getCreator().flatMap(Sponge.getServer()::getPlayer).ifPresent(player -> { + player.sendMessage(Text.of(Toolbox.getTextPrefix(), Toolbox.convertColor(message.replace("[ID]", Toolbox.getRootType(mixinEntity).getId())))); + }); + }); + } + + private boolean shouldRemove(IMixinEntity mixinEntity) { + if (!(mixinEntity instanceof Item || mixinEntity instanceof Minecart) || mixinEntity.isRemoved()) { + return false; + } + + return Sledgehammer.getInstance().getConfig() + .map(Config::getMixinCategory) + .map(MixinCategory::getItemTeleportWhitelist) + .map(list -> !list.contains(Toolbox.getRootType(mixinEntity).getId())).orElse(false); + } +} \ 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 e8cf396..1f2cfbe 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.15"; + public static final String PLUGIN_VERSION = "1.12.2-1.2.16"; 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/"; diff --git a/src/main/java/io/github/lxgaming/sledgehammer/util/Toolbox.java b/src/main/java/io/github/lxgaming/sledgehammer/util/Toolbox.java index 82a40b9..68ec5c2 100644 --- a/src/main/java/io/github/lxgaming/sledgehammer/util/Toolbox.java +++ b/src/main/java/io/github/lxgaming/sledgehammer/util/Toolbox.java @@ -18,6 +18,9 @@ import net.minecraft.launchwrapper.Launch; import org.apache.commons.lang3.StringUtils; +import org.spongepowered.api.CatalogType; +import org.spongepowered.api.entity.Entity; +import org.spongepowered.api.entity.Item; import org.spongepowered.api.text.Text; import org.spongepowered.api.text.action.TextAction; import org.spongepowered.api.text.action.TextActions; @@ -66,6 +69,14 @@ public static Text convertColor(String string) { return TextSerializers.FORMATTING_CODE.deserialize(string); } + public static CatalogType getRootType(Entity entity) { + if (entity instanceof Item) { + return ((Item) entity).getItemType(); + } + + return entity.getType(); + } + public static boolean isForgeEnvironment() { return Toolbox.isClassPresent("net.minecraftforge.fml.relauncher.CoreModManager") || Toolbox.isClassPresent("cpw.mods.fml.relauncher.CoreModManager"); } diff --git a/src/main/resources/mixins.sledgehammer.core.json b/src/main/resources/mixins.sledgehammer.core.json index 9ceb0fe..8198fa9 100644 --- a/src/main/resources/mixins.sledgehammer.core.json +++ b/src/main/resources/mixins.sledgehammer.core.json @@ -10,6 +10,7 @@ "advancements.MixinAdvancementManager", "block.MixinBlockGrass", "block.MixinBlockIce", + "entity.MixinEntity_Teleport", "network.MixinNetHandlerPlayServer", "network.MixinNetworkManager", "network.MixinNetworkSystem",