Skip to content

Commit

Permalink
Optimize EntityTeleport fix
Browse files Browse the repository at this point in the history
Now supports Minecarts
Attempts to save entities by cancelling setPortal
  • Loading branch information
LXGaming committed Dec 12, 2018
1 parent c57709c commit 87c8779
Show file tree
Hide file tree
Showing 11 changed files with 126 additions and 129 deletions.
16 changes: 9 additions & 7 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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")
}

Expand All @@ -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")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand All @@ -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<String> spongeItemWhitelist = Toolbox.newArrayList("draconicevolution:ender_energy_manipulator");

@Setting(value = "sponge-phase", comment = "Fixes https://github.com/SpongePowered/SpongeForge/issues/2355")
private boolean spongePhase = false;

Expand All @@ -69,14 +60,6 @@ public boolean isSpongeDeath() {
return spongeDeath;
}

public boolean isSpongeItemTeleport() {
return spongeItemTeleport;
}

public List<String> getSpongeItemWhitelist() {
return spongeItemWhitelist;
}

public boolean isSpongePhase() {
return spongePhase;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down Expand Up @@ -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<String> itemTeleportWhitelist = Lists.newArrayList("draconicevolution:ender_energy_manipulator");

@Setting(value = "traveling-merchant", comment = "Fixes https://github.com/Daveyx0/PrimitiveMobs/issues/59")
private boolean travelingMerchant = false;

Expand Down Expand Up @@ -95,6 +104,14 @@ public boolean isPacketSpam() {
return packetSpam;
}

public boolean isItemTeleport() {
return itemTeleport;
}

public List<String> getItemTeleportWhitelist() {
return itemTeleportWhitelist;
}

public boolean isTravelingMerchant() {
return travelingMerchant;
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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<Entity> 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<Entity> 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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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/";
Expand Down
11 changes: 11 additions & 0 deletions src/main/java/io/github/lxgaming/sledgehammer/util/Toolbox.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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");
}
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/mixins.sledgehammer.core.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"advancements.MixinAdvancementManager",
"block.MixinBlockGrass",
"block.MixinBlockIce",
"entity.MixinEntity_Teleport",
"network.MixinNetHandlerPlayServer",
"network.MixinNetworkManager",
"network.MixinNetworkSystem",
Expand Down

0 comments on commit 87c8779

Please sign in to comment.