Skip to content

Commit

Permalink
Fix heavy weapon detection not working
Browse files Browse the repository at this point in the history
Closes #1209
Closes #1204
  • Loading branch information
Shadows-of-Fire committed Mar 24, 2024
1 parent 2fa4c77 commit 39467d5
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,24 +16,19 @@

import dev.shadowsoffire.apotheosis.adventure.AdventureConfig;
import dev.shadowsoffire.placebo.codec.PlaceboCodecs;
import net.minecraft.server.MinecraftServer;
import net.minecraft.util.ExtraCodecs;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.entity.EquipmentSlot;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.ai.attributes.Attributes;
import net.minecraft.world.entity.monster.Zombie;
import net.minecraft.world.item.BlockItem;
import net.minecraft.world.item.BowItem;
import net.minecraft.world.item.CrossbowItem;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.Items;
import net.minecraft.world.item.TridentItem;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.AbstractSkullBlock;
import net.minecraftforge.common.ToolActions;
import net.minecraftforge.registries.ForgeRegistries;
import net.minecraftforge.server.ServerLifecycleHooks;

public final class LootCategory {

Expand Down Expand Up @@ -193,28 +188,6 @@ private static EquipmentSlot[] arr(EquipmentSlot... s) {
return s;
}

private static class ShieldBreakerTest implements Predicate<ItemStack> {

private Zombie attacker, holder;

@Override
public boolean test(ItemStack t) {
try {
MinecraftServer server = ServerLifecycleHooks.getCurrentServer();
if (this.attacker == null && server != null) {
this.attacker = new Zombie(server.getLevel(Level.OVERWORLD));
this.holder = new Zombie(server.getLevel(Level.OVERWORLD));
this.holder.setItemInHand(InteractionHand.OFF_HAND, new ItemStack(Items.SHIELD));
}
return t.canDisableShield(this.holder.getOffhandItem(), this.holder, this.attacker);
}
catch (Exception ex) {
return false;
}
}

}

private static Predicate<ItemStack> armorSlot(EquipmentSlot slot) {
return stack -> {
if (stack.is(Items.CARVED_PUMPKIN) || stack.getItem() instanceof BlockItem bi && bi.getBlock() instanceof AbstractSkullBlock) return false;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package dev.shadowsoffire.apotheosis.adventure.loot;

import java.util.IdentityHashMap;
import java.util.Map;
import java.util.function.Predicate;

import dev.shadowsoffire.apotheosis.Apotheosis;
import dev.shadowsoffire.apotheosis.adventure.AdventureModule;
import net.minecraft.client.Minecraft;
import net.minecraft.server.MinecraftServer;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.entity.monster.Zombie;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.Items;
import net.minecraft.world.level.Level;
import net.minecraftforge.event.level.LevelEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod.EventBusSubscriber;
import net.minecraftforge.fml.common.Mod.EventBusSubscriber.Bus;
import net.minecraftforge.server.ServerLifecycleHooks;

// This is kind of horrible, but we have to try and make entities to call the method and not leak those entities
// TODO: Delete heavy weapons
@EventBusSubscriber(modid = Apotheosis.MODID, bus = Bus.FORGE)
class ShieldBreakerTest implements Predicate<ItemStack> {

private static Map<Level, Zombies> zombieCache = new IdentityHashMap<>();

@Override
public boolean test(ItemStack t) {
try {
MinecraftServer server = ServerLifecycleHooks.getCurrentServer();
Level level = server != null ? server.getLevel(Level.OVERWORLD) : Client.getLevel();
Zombies zombies = zombieCache.computeIfAbsent(level, Zombies::new);
return t.canDisableShield(zombies.target.getOffhandItem(), zombies.target, zombies.attacker);
}
catch (Exception ex) {
AdventureModule.LOGGER.error("Failed to execute ShieldBreakerTest", ex);
return false;
}
}

@SubscribeEvent
public static void unload(LevelEvent.Unload e) {
zombieCache.remove(e.getLevel());
}

private record Zombies(Zombie attacker, Zombie target) {

public Zombies(Level level) {
this(new Zombie(level), new Zombie(level));
this.target.setItemInHand(InteractionHand.OFF_HAND, new ItemStack(Items.SHIELD));
}

}

private static class Client {
static Level getLevel() {
return Minecraft.getInstance().level;
}
}

}

0 comments on commit 39467d5

Please sign in to comment.