Skip to content

Commit

Permalink
fix: 🐛 Fix mob charms and twilight cloak to not crash with some of th…
Browse files Browse the repository at this point in the history
…e entities
  • Loading branch information
P3pp3rF1y committed Apr 1, 2024
1 parent 11fad74 commit 2f1aa8c
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 38 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
org.gradle.jvmargs=-Xmx2048m
minecraft_version=1.20.1
forge_version=47.1.5
mod_version=2.0.38
mod_version=2.0.39
jei_mc_version=1.20.1-forge
jei_version=15.1.0.19
curios_version=5.2.0-beta.3+1.20.1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,21 @@
import reliquary.items.util.ICuriosItem;
import top.theillusivec4.curios.api.CuriosApi;

import java.util.Set;

public class CuriosCharmInventoryHandler extends MobCharmItem.CharmInventoryHandler {
@Override
public boolean playerHasMobCharm(Player player, MobCharmDefinition charmDefinition) {
if (super.playerHasMobCharm(player, charmDefinition)) {
return true;
}
return CuriosApi.getCuriosHelper().getCuriosHandler(player).map(handler -> handler.getStacksHandler(ICuriosItem.Type.BELT.getIdentifier()).map(stackHandler -> {
protected Set<String> getCharmRegistryNames(Player player) {
Set<String> ret = super.getCharmRegistryNames(player);
CuriosApi.getCuriosHelper().getCuriosHandler(player).resolve().flatMap(handler -> handler.getStacksHandler(ICuriosItem.Type.BELT.getIdentifier())).ifPresent(stackHandler -> {
for (int slot = 0; slot < stackHandler.getSlots(); slot++) {
ItemStack baubleStack = stackHandler.getStacks().getStackInSlot(slot);
if (!baubleStack.isEmpty() && baubleStack.getItem() == ModItems.MOB_CHARM_BELT.get() && ModItems.MOB_CHARM_BELT.get().hasCharm(baubleStack, charmDefinition.getRegistryName())) {
return true;
if (!baubleStack.isEmpty() && baubleStack.getItem() == ModItems.MOB_CHARM_BELT.get()) {
ret.addAll(ModItems.MOB_CHARM_BELT.get().getCharmRegistryNames(baubleStack));
}
}
return false;
}).orElse(false)).orElse(false);
});
return ret;
}

@Override
Expand Down
13 changes: 13 additions & 0 deletions src/main/java/reliquary/items/MobCharmBeltItem.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
import reliquary.items.util.ICuriosItem;
import reliquary.reference.Settings;

import java.util.HashSet;
import java.util.Set;

public class MobCharmBeltItem extends ItemBase implements ICuriosItem {
private static final String SLOTS_TAG = "Slots";

Expand Down Expand Up @@ -177,4 +180,14 @@ ItemStack damageCharm(Player player, ItemStack belt, String entityRegistryName)

return ItemStack.EMPTY;
}

public Set<String> getCharmRegistryNames(ItemStack slotStack) {
Set<String> ret = new HashSet<>();
ListTag mobCharms = slotStack.getOrCreateTag().getList(SLOTS_TAG, Tag.TAG_COMPOUND);
for (int i = 0; i < mobCharms.size(); i++) {
ItemStack charmStack = ItemStack.of(mobCharms.getCompound(i));
ret.add(MobCharmItem.getEntityRegistryName(charmStack));
}
return ret;
}
}
26 changes: 8 additions & 18 deletions src/main/java/reliquary/items/MobCharmDefinition.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,20 @@ public class MobCharmDefinition {
static final MobCharmDefinition WITHER_SKELETON = new MobCharmDefinition("minecraft:wither_skeleton", ModItems.WITHERED_RIB.get(), "minecraft:wither_skeleton");
static final MobCharmDefinition CREEPER = new MobCharmDefinition("minecraft:creeper", ModItems.CATALYZING_GLAND.get(), "minecraft:creeper");
static final MobCharmDefinition WITCH = new MobCharmDefinition("minecraft:witch", ModItems.WITCH_HAT.get(), "minecraft:witch");
static final MobCharmDefinition ZOMBIFIED_PIGLIN = new MobCharmDefinition("minecraft:zombified_piglin", ModItems.ZOMBIE_HEART.get(), "minecraft:zombified_piglin").setResetTargetInLivingTickEvent(true);
static final MobCharmDefinition ZOMBIFIED_PIGLIN = new MobCharmDefinition("minecraft:zombified_piglin", ModItems.ZOMBIE_HEART.get(), "minecraft:zombified_piglin");
static final MobCharmDefinition CAVE_SPIDER = new MobCharmDefinition("minecraft:cave_spider", ModItems.CHELICERAE.get(), "minecraft:cave_spider");
static final MobCharmDefinition SPIDER = new MobCharmDefinition("minecraft:spider", ModItems.CHELICERAE.get(), "minecraft:spider");
static final MobCharmDefinition ENDERMAN = new MobCharmDefinition("minecraft:enderman", ModItems.NEBULOUS_HEART.get(), "minecraft:enderman").setResetTargetInLivingTickEvent(true);
static final MobCharmDefinition GHAST = new MobCharmDefinition("minecraft:ghast", ModItems.CATALYZING_GLAND.get(), "minecraft:ghast").setResetTargetInLivingTickEvent(true);
static final MobCharmDefinition SLIME = new MobCharmDefinition("minecraft:slime", ModItems.SLIME_PEARL.get(), "minecraft:slime").setResetTargetInLivingTickEvent(true);
static final MobCharmDefinition MAGMA_CUBE = new MobCharmDefinition("minecraft:magma_cube", ModItems.MOLTEN_CORE.get(), "minecraft:magma_cube").setResetTargetInLivingTickEvent(true);
static final MobCharmDefinition ENDERMAN = new MobCharmDefinition("minecraft:enderman", ModItems.NEBULOUS_HEART.get(), "minecraft:enderman");
static final MobCharmDefinition GHAST = new MobCharmDefinition("minecraft:ghast", ModItems.CATALYZING_GLAND.get(), "minecraft:ghast");
static final MobCharmDefinition SLIME = new MobCharmDefinition("minecraft:slime", ModItems.SLIME_PEARL.get(), "minecraft:slime");
static final MobCharmDefinition MAGMA_CUBE = new MobCharmDefinition("minecraft:magma_cube", ModItems.MOLTEN_CORE.get(), "minecraft:magma_cube");
static final MobCharmDefinition BLAZE = new MobCharmDefinition("minecraft:blaze", ModItems.MOLTEN_CORE.get(), "minecraft:blaze");
static final MobCharmDefinition GUARDIAN = new MobCharmDefinition("minecraft:guardian", ModItems.GUARDIAN_SPIKE.get(), "minecraft:guardian");
static final MobCharmDefinition PIGLIN = new MobCharmDefinition("minecraft:piglin", null, "minecraft:piglin").setResetTargetInLivingTickEvent(true);
static final MobCharmDefinition PIGLIN_BRUTE = new MobCharmDefinition("minecraft:piglin_brute", null, "minecraft:piglin_brute").setResetTargetInLivingTickEvent(true);
static final MobCharmDefinition HOGLIN = new MobCharmDefinition("minecraft:hoglin", null, "minecraft:hoglin").setResetTargetInLivingTickEvent(true);
static final MobCharmDefinition PIGLIN = new MobCharmDefinition("minecraft:piglin", null, "minecraft:piglin");
static final MobCharmDefinition PIGLIN_BRUTE = new MobCharmDefinition("minecraft:piglin_brute", null, "minecraft:piglin_brute");
static final MobCharmDefinition HOGLIN = new MobCharmDefinition("minecraft:hoglin", null, "minecraft:hoglin");

private final Set<String> applicableToEntities = new HashSet<>();
private boolean resetTargetInLivingTickEvent = false;
private final String registryName;
private final Item repairItem;
private boolean dynamicallyCreated = false;
Expand All @@ -49,15 +48,6 @@ public String getRegistryName() {
return registryName;
}

public MobCharmDefinition setResetTargetInLivingTickEvent(boolean resetTargetInLivingTickEvent) {
this.resetTargetInLivingTickEvent = resetTargetInLivingTickEvent;
return this;
}

public boolean resetTargetInLivingTickEvent() {
return resetTargetInLivingTickEvent;
}

public Set<String> getEntities() {
return applicableToEntities;
}
Expand Down
34 changes: 25 additions & 9 deletions src/main/java/reliquary/items/MobCharmItem.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,7 @@
import reliquary.util.WorldHelper;

import javax.annotation.Nullable;
import java.util.List;
import java.util.Optional;
import java.util.*;
import java.util.function.Consumer;

public class MobCharmItem extends ItemBase {
Expand Down Expand Up @@ -96,7 +95,7 @@ private void onEntityTargetedEvent(LivingChangeTargetEvent event) {

MobCharmRegistry.getCharmDefinitionFor(entity).ifPresent(charmDefinition -> {
if (isMobCharmPresent(player, charmDefinition)) {
event.setNewTarget(null);
event.setCanceled(true);
}
});
}
Expand All @@ -119,7 +118,7 @@ private void onLivingUpdate(LivingEvent.LivingTickEvent event) {
}

Player finalPlayer = player;
MobCharmRegistry.getCharmDefinitionFor(entity).filter(MobCharmDefinition::resetTargetInLivingTickEvent).ifPresent(charmDefinition -> {
MobCharmRegistry.getCharmDefinitionFor(entity).ifPresent(charmDefinition -> {
if (isMobCharmPresent(finalPlayer, charmDefinition)) {
MobHelper.resetTarget(entity, true);
}
Expand Down Expand Up @@ -225,20 +224,37 @@ public void setCharmInventoryHandler(CharmInventoryHandler charmInventoryHandler
}

public static class CharmInventoryHandler {
public boolean playerHasMobCharm(Player player, MobCharmDefinition charmDefinition) {
String registryName = charmDefinition.getRegistryName();
private static long lastCharmCacheTime = -1;
private static final Map<UUID, Set<String>> charmsInInventoryCache = new HashMap<>();

protected Set<String> getCharmRegistryNames(Player player) {
Set<String> ret = new HashSet<>();
for (ItemStack slotStack : player.getInventory().items) {
if (slotStack.isEmpty()) {
continue;
}
if (ModItems.MOB_CHARM.get().isCharmOrBeltFor(slotStack, registryName)) {
return true;
if (slotStack.getItem() == ModItems.MOB_CHARM.get()) {
ret.add(getEntityRegistryName(slotStack));
}
if (slotStack.getItem() == ModItems.MOB_CHARM_BELT.get()) {
ret.addAll(ModItems.MOB_CHARM_BELT.get().getCharmRegistryNames(slotStack));
}
}
return false;
return ret;
}

public boolean playerHasMobCharm(Player player, MobCharmDefinition charmDefinition) {
String registryName = charmDefinition.getRegistryName();

if (lastCharmCacheTime != player.level().getGameTime()) {
lastCharmCacheTime = player.level().getGameTime();
charmsInInventoryCache.clear();
charmsInInventoryCache.put(player.getUUID(), getCharmRegistryNames(player));
}
return charmsInInventoryCache.get(player.getUUID()).contains(registryName);
}


public boolean damagePlayersMobCharm(Player player, String entityRegistryName) {
if (player.isCreative()) {
return true;
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/reliquary/items/TwilightCloakItem.java
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public void onWornTick(ItemStack twilightCloak, LivingEntity player) {

private void onEntityTargetedEvent(LivingChangeTargetEvent event) {
if (shouldResetTarget(event.getNewTarget())) {
event.setNewTarget(null);
event.setCanceled(true);
}
}

Expand Down

0 comments on commit 2f1aa8c

Please sign in to comment.