From f9499a729acbf5f7f291a792e54c2bf5fbb953cb Mon Sep 17 00:00:00 2001 From: IzzelAliz Date: Thu, 8 Oct 2020 13:54:17 +0800 Subject: [PATCH] Port fix for #46 #51 #59 and #61 from 1.15 --- .../core/entity/ai/goal/TemptGoalMixin.java | 3 ++- .../player/ServerPlayerEntityMixin.java | 10 ++++--- .../mixin/core/loot/LootParametersMixin.java | 4 +-- .../core/world/server/ServerWorldMixin.java | 5 ++++ .../common/mod/ArclightConstants.java | 5 ++-- .../common/mod/server/BukkitRegistry.java | 26 +++++-------------- .../server/event/EntityEventDispatcher.java | 2 ++ 7 files changed, 27 insertions(+), 28 deletions(-) diff --git a/arclight-common/src/main/java/io/izzel/arclight/common/mixin/core/entity/ai/goal/TemptGoalMixin.java b/arclight-common/src/main/java/io/izzel/arclight/common/mixin/core/entity/ai/goal/TemptGoalMixin.java index 42f457ea..c34fb432 100644 --- a/arclight-common/src/main/java/io/izzel/arclight/common/mixin/core/entity/ai/goal/TemptGoalMixin.java +++ b/arclight-common/src/main/java/io/izzel/arclight/common/mixin/core/entity/ai/goal/TemptGoalMixin.java @@ -8,6 +8,7 @@ import org.bukkit.craftbukkit.v.entity.CraftHumanEntity; import org.bukkit.craftbukkit.v.event.CraftEventFactory; import org.bukkit.event.entity.EntityTargetEvent; import org.bukkit.event.entity.EntityTargetLivingEntityEvent; +import org.objectweb.asm.Opcodes; import org.spongepowered.asm.mixin.Final; import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.Shadow; @@ -24,7 +25,7 @@ public abstract class TemptGoalMixin { @Shadow @Final protected CreatureEntity creature; // @formatter:on - @Inject(method = "shouldExecute", cancellable = true, at = @At(value = "INVOKE_ASSIGN", ordinal = 0, target = "Lnet/minecraft/world/World;getClosestPlayer(Lnet/minecraft/entity/EntityPredicate;Lnet/minecraft/entity/LivingEntity;)Lnet/minecraft/entity/player/PlayerEntity;")) + @Inject(method = "shouldExecute", cancellable = true, at = @At(value = "FIELD", shift = At.Shift.AFTER, opcode = Opcodes.PUTFIELD, target = "Lnet/minecraft/entity/ai/goal/TemptGoal;closestPlayer:Lnet/minecraft/entity/player/PlayerEntity;")) public void arclight$tempt(CallbackInfoReturnable cir) { boolean tempt = this.closestPlayer != null && (this.isTempting(this.closestPlayer.getHeldItemMainhand()) || this.isTempting(this.closestPlayer.getHeldItemOffhand())); if (tempt) { diff --git a/arclight-common/src/main/java/io/izzel/arclight/common/mixin/core/entity/player/ServerPlayerEntityMixin.java b/arclight-common/src/main/java/io/izzel/arclight/common/mixin/core/entity/player/ServerPlayerEntityMixin.java index d6b675c5..718b2945 100644 --- a/arclight-common/src/main/java/io/izzel/arclight/common/mixin/core/entity/player/ServerPlayerEntityMixin.java +++ b/arclight-common/src/main/java/io/izzel/arclight/common/mixin/core/entity/player/ServerPlayerEntityMixin.java @@ -1,6 +1,5 @@ package io.izzel.arclight.common.mixin.core.entity.player; -import com.google.common.collect.Lists; import com.mojang.datafixers.util.Either; import io.izzel.arclight.common.bridge.block.PortalInfoBridge; import io.izzel.arclight.common.bridge.entity.EntityBridge; @@ -320,13 +319,16 @@ public abstract class ServerPlayerEntityMixin extends PlayerEntityMixin implemen return; } boolean keepInventory = this.world.getGameRules().getBoolean(GameRules.KEEP_INVENTORY) || this.isSpectator(); - List loot = new ArrayList<>(); - this.captureDrops(new ArrayList<>()); this.spawnDrops(damagesource); ITextComponent defaultMessage = this.getCombatTracker().getDeathMessage(); String deathmessage = defaultMessage.getString(); - PlayerDeathEvent event = CraftEventFactory.callPlayerDeathEvent((ServerPlayerEntity) (Object) this, Lists.transform(loot, (ItemEntity entity) -> CraftItemStack.asCraftMirror(entity.getItem())), deathmessage, keepInventory); + List loot = new ArrayList<>(); + for (ItemEntity entity : this.captureDrops(null)) { + CraftItemStack craftItemStack = CraftItemStack.asCraftMirror(entity.getItem()); + loot.add(craftItemStack); + } + PlayerDeathEvent event = CraftEventFactory.callPlayerDeathEvent((ServerPlayerEntity) (Object) this, loot, deathmessage, keepInventory); if (this.openContainer != this.container) { this.closeScreen(); } diff --git a/arclight-common/src/main/java/io/izzel/arclight/common/mixin/core/loot/LootParametersMixin.java b/arclight-common/src/main/java/io/izzel/arclight/common/mixin/core/loot/LootParametersMixin.java index 0ecc93e2..7eb43089 100644 --- a/arclight-common/src/main/java/io/izzel/arclight/common/mixin/core/loot/LootParametersMixin.java +++ b/arclight-common/src/main/java/io/izzel/arclight/common/mixin/core/loot/LootParametersMixin.java @@ -1,12 +1,12 @@ package io.izzel.arclight.common.mixin.core.loot; -import io.izzel.arclight.common.mod.ArclightConstants; import net.minecraft.loot.LootParameter; import net.minecraft.loot.LootParameters; +import net.minecraft.util.ResourceLocation; import org.spongepowered.asm.mixin.Mixin; @Mixin(LootParameters.class) public class LootParametersMixin { - private static final LootParameter LOOTING_MOD = ArclightConstants.LOOTING_MOD; + private static final LootParameter LOOTING_MOD = new LootParameter<>(new ResourceLocation("bukkit:looting_mod")); } diff --git a/arclight-common/src/main/java/io/izzel/arclight/common/mixin/core/world/server/ServerWorldMixin.java b/arclight-common/src/main/java/io/izzel/arclight/common/mixin/core/world/server/ServerWorldMixin.java index efcafae4..e870965b 100644 --- a/arclight-common/src/main/java/io/izzel/arclight/common/mixin/core/world/server/ServerWorldMixin.java +++ b/arclight-common/src/main/java/io/izzel/arclight/common/mixin/core/world/server/ServerWorldMixin.java @@ -31,6 +31,7 @@ import net.minecraft.tileentity.TileEntity; import net.minecraft.util.DamageSource; import net.minecraft.util.IProgressUpdate; import net.minecraft.util.RegistryKey; +import net.minecraft.util.ResourceLocation; import net.minecraft.util.math.BlockPos; import net.minecraft.world.DimensionType; import net.minecraft.world.Explosion; @@ -383,6 +384,10 @@ public abstract class ServerWorldMixin extends WorldMixin implements ServerWorld } private TileEntity fixTileEntity(BlockPos pos, BlockState state, Block type, TileEntity found) { + ResourceLocation registryName = found.getType().getRegistryName(); + if (registryName == null || !registryName.getNamespace().equals("minecraft")) { + return found; + } this.getServer().getLogger().log(Level.SEVERE, "Block at {0}, {1}, {2} is {3} but has {4}" + ". " + "Bukkit will attempt to fix this, but there may be additional damage that we cannot recover.", new Object[]{pos.getX(), pos.getY(), pos.getZ(), type, found}); diff --git a/arclight-common/src/main/java/io/izzel/arclight/common/mod/ArclightConstants.java b/arclight-common/src/main/java/io/izzel/arclight/common/mod/ArclightConstants.java index eb807898..4bdc1fe4 100644 --- a/arclight-common/src/main/java/io/izzel/arclight/common/mod/ArclightConstants.java +++ b/arclight-common/src/main/java/io/izzel/arclight/common/mod/ArclightConstants.java @@ -2,15 +2,16 @@ package io.izzel.arclight.common.mod; import com.google.common.collect.ImmutableList; import io.izzel.arclight.api.EnumHelper; +import io.izzel.arclight.api.Unsafe; import net.minecraft.loot.LootParameter; -import net.minecraft.util.ResourceLocation; +import net.minecraft.loot.LootParameters; import org.bukkit.TreeType; public class ArclightConstants { public static final TreeType MOD = EnumHelper.addEnum(TreeType.class, "MOD", ImmutableList.of(), ImmutableList.of()); - public static final LootParameter LOOTING_MOD = new LootParameter<>(new ResourceLocation("bukkit:looting_mod")); + public static final LootParameter LOOTING_MOD = Unsafe.getStatic(LootParameters.class, "LOOTING_MOD"); /** * Arclight marker magic value for non-used custom dimension diff --git a/arclight-common/src/main/java/io/izzel/arclight/common/mod/server/BukkitRegistry.java b/arclight-common/src/main/java/io/izzel/arclight/common/mod/server/BukkitRegistry.java index 001b0e74..c08495d3 100644 --- a/arclight-common/src/main/java/io/izzel/arclight/common/mod/server/BukkitRegistry.java +++ b/arclight-common/src/main/java/io/izzel/arclight/common/mod/server/BukkitRegistry.java @@ -54,13 +54,13 @@ public class BukkitRegistry { private static final List> MAT_CTOR = ImmutableList.of(int.class); private static final List> ENTITY_CTOR = ImmutableList.of(String.class, Class.class, int.class); private static final List> ENV_CTOR = ImmutableList.of(int.class); - private static final Map BY_NAME = getStatic(Material.class, "BY_NAME"); - private static final Map BLOCK_MATERIAL = getStatic(CraftMagicNumbers.class, "BLOCK_MATERIAL"); - private static final Map ITEM_MATERIAL = getStatic(CraftMagicNumbers.class, "ITEM_MATERIAL"); - private static final Map MATERIAL_ITEM = getStatic(CraftMagicNumbers.class, "MATERIAL_ITEM"); - private static final Map MATERIAL_BLOCK = getStatic(CraftMagicNumbers.class, "MATERIAL_BLOCK"); - private static final Map ENTITY_NAME_MAP = getStatic(EntityType.class, "NAME_MAP"); - private static final Map ENVIRONMENT_MAP = getStatic(World.Environment.class, "lookup"); + private static final Map BY_NAME = Unsafe.getStatic(Material.class, "BY_NAME"); + private static final Map BLOCK_MATERIAL = Unsafe.getStatic(CraftMagicNumbers.class, "BLOCK_MATERIAL"); + private static final Map ITEM_MATERIAL = Unsafe.getStatic(CraftMagicNumbers.class, "ITEM_MATERIAL"); + private static final Map MATERIAL_ITEM = Unsafe.getStatic(CraftMagicNumbers.class, "MATERIAL_ITEM"); + private static final Map MATERIAL_BLOCK = Unsafe.getStatic(CraftMagicNumbers.class, "MATERIAL_BLOCK"); + private static final Map ENTITY_NAME_MAP = Unsafe.getStatic(EntityType.class, "NAME_MAP"); + private static final Map ENVIRONMENT_MAP = Unsafe.getStatic(World.Environment.class, "lookup"); static final BiMap, World.Environment> DIM_MAP = HashBiMap.create(ImmutableMap., World.Environment>builder() .put(DimensionType.OVERWORLD, World.Environment.NORMAL) @@ -264,18 +264,6 @@ public class BukkitRegistry { return ArclightConfig.spec().getCompat().getEntity(location.toString()).orElse(EntityPropertySpec.EMPTY); } - private static T getStatic(Class cl, String name) { - try { - Unsafe.ensureClassInitialized(cl); - Field field = cl.getDeclaredField(name); - Object materialByNameBase = Unsafe.staticFieldBase(field); - long materialByNameOffset = Unsafe.staticFieldOffset(field); - return (T) Unsafe.getObject(materialByNameBase, materialByNameOffset); - } catch (Exception e) { - return null; - } - } - private static void putStatic(Class cl, String name, Object o) { try { Unsafe.ensureClassInitialized(cl); diff --git a/arclight-common/src/main/java/io/izzel/arclight/common/mod/server/event/EntityEventDispatcher.java b/arclight-common/src/main/java/io/izzel/arclight/common/mod/server/event/EntityEventDispatcher.java index 3461c7da..914017f0 100644 --- a/arclight-common/src/main/java/io/izzel/arclight/common/mod/server/event/EntityEventDispatcher.java +++ b/arclight-common/src/main/java/io/izzel/arclight/common/mod/server/event/EntityEventDispatcher.java @@ -23,6 +23,8 @@ public class EntityEventDispatcher { @SubscribeEvent(receiveCanceled = true) public void onLivingDeath(LivingDropsEvent event) { if (event.getEntityLiving() instanceof ServerPlayerEntity) { + // recapture for ServerPlayerEntityMixin#onDeath + event.getEntityLiving().captureDrops(event.getDrops()); // handled at ServerPlayerEntityMixin event.setCanceled(true); return;