Skip to content

Commit

Permalink
refactor: ♻️ Improve performance of mob charm checks
Browse files Browse the repository at this point in the history
  • Loading branch information
P3pp3rF1y committed Apr 1, 2024
1 parent b36b04b commit 7c85103
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 19 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ loader_version_range=[2,)
mod_id=reliquary
mod_name=Reliquary
mod_license=GNU General Public License v3.0
mod_version=2.0.36
mod_version=2.0.37
mod_group_id=reliquary
mod_authors=x3n0ph0b3, TheMike, P3pp3rF1y
mod_description=Two words: magical swag. Oh, and a gun.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,25 @@
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.ItemStack;
import reliquary.init.ModItems;
import reliquary.items.MobCharmDefinition;
import reliquary.items.MobCharmItem;
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.getCuriosInventory(player).map(handler -> handler.getStacksHandler(ICuriosItem.Type.BELT.getIdentifier()).map(stackHandler -> {
protected Set<String> getCharmRegistryNames(Player player) {
Set<String> ret = super.getCharmRegistryNames(player);
CuriosApi.getCuriosInventory(player).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 All @@ -39,7 +38,7 @@ public boolean damagePlayersMobCharm(ServerPlayer player, String entityRegistryN
continue;
}

if (damageMobCharmInBelt((ServerPlayer) player, entityRegistryName, baubleStack)) {
if (damageMobCharmInBelt(player, entityRegistryName, baubleStack)) {
return true;
}
}
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 @@ -18,6 +18,9 @@
import reliquary.items.util.ICuriosItem;
import reliquary.reference.Config;

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 @@ -176,4 +179,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;
}
}
29 changes: 22 additions & 7 deletions src/main/java/reliquary/items/MobCharmItem.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,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 @@ -216,18 +215,34 @@ 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(ServerPlayer player, String entityRegistryName) {
Expand Down

0 comments on commit 7c85103

Please sign in to comment.