Skip to content

Commit

Permalink
fix index out of bounds errors with slot modifiers, closes #221
Browse files Browse the repository at this point in the history
  • Loading branch information
TheIllusiveC4 committed Feb 15, 2022
1 parent 1b0b2b2 commit 8a284e6
Show file tree
Hide file tree
Showing 10 changed files with 63 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import net.minecraft.client.renderer.MultiBufferSource;
import net.minecraft.client.renderer.entity.RenderLayerParent;
import net.minecraft.client.renderer.entity.layers.RenderLayer;
import net.minecraft.core.NonNullList;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.item.ItemStack;
import top.theillusivec4.curios.api.CuriosApi;
Expand Down Expand Up @@ -55,15 +56,16 @@ public void render(@Nonnull PoseStack matrixStack, @Nonnull MultiBufferSource re
for (int i = 0; i < stackHandler.getSlots(); i++) {
ItemStack stack = cosmeticStacksHandler.getStackInSlot(i);
boolean cosmetic = true;
NonNullList<Boolean> renderStates = stacksHandler.getRenders();
boolean renderable = renderStates.size() > i && renderStates.get(i);

if (stack.isEmpty() && stacksHandler.getRenders().get(i)) {
if (stack.isEmpty() && renderable) {
stack = stackHandler.getStackInSlot(i);
cosmetic = false;
}

if (!stack.isEmpty()) {
SlotContext slotContext =
new SlotContext(id, livingEntity, i, cosmetic, stacksHandler.getRenders().get(i));
SlotContext slotContext = new SlotContext(id, livingEntity, i, cosmetic, renderable);
ItemStack finalStack = stack;
CuriosRendererRegistry.getRenderer(stack.getItem()).ifPresent(
renderer -> renderer
Expand Down
19 changes: 10 additions & 9 deletions src/main/java/top/theillusivec4/curios/common/CuriosHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import java.util.function.Predicate;
import java.util.stream.Collectors;
import javax.annotation.Nonnull;
import net.minecraft.core.NonNullList;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.ListTag;
import net.minecraft.resources.ResourceLocation;
Expand Down Expand Up @@ -120,9 +121,9 @@ public Optional<SlotResult> findFirstCurio(@Nonnull LivingEntity livingEntity,
ItemStack stack = stackHandler.getStackInSlot(i);

if (!stack.isEmpty() && filter.test(stack)) {
return new SlotResult(
new SlotContext(id, livingEntity, i, false, stacksHandler.getRenders().get(i)),
stack);
NonNullList<Boolean> renderStates = stacksHandler.getRenders();
return new SlotResult(new SlotContext(id, livingEntity, i, false,
renderStates.size() > i && renderStates.get(i)), stack);
}
}
}
Expand Down Expand Up @@ -151,9 +152,9 @@ public List<SlotResult> findCurios(@Nonnull LivingEntity livingEntity,
ItemStack stack = stackHandler.getStackInSlot(i);

if (!stack.isEmpty() && filter.test(stack)) {
result.add(new SlotResult(
new SlotContext(id, livingEntity, i, false, stacksHandler.getRenders().get(i)),
stack));
NonNullList<Boolean> renderStates = stacksHandler.getRenders();
result.add(new SlotResult(new SlotContext(id, livingEntity, i, false,
renderStates.size() > i && renderStates.get(i)), stack));
}
}
}
Expand All @@ -178,9 +179,9 @@ public List<SlotResult> findCurios(@NotNull LivingEntity livingEntity, String...
ItemStack stack = stackHandler.getStackInSlot(i);

if (!stack.isEmpty()) {
result.add(new SlotResult(
new SlotContext(id, livingEntity, i, false, stacksHandler.getRenders().get(i)),
stack));
NonNullList<Boolean> renderStates = stacksHandler.getRenders();
result.add(new SlotResult(new SlotContext(id, livingEntity, i, false,
renderStates.size() > i && renderStates.get(i)), stack));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,9 +180,12 @@ public int getFortuneLevel(@Nullable LootContext lootContext) {
for (int i = 0; i < stacks.getSlots(); i++) {
final int index = i;
fortuneLevel += CuriosApi.getCuriosHelper().getCurio(stacks.getStackInSlot(i)).map(
curio -> curio.getFortuneLevel(
new SlotContext(entry.getKey(), this.wearer, index, false,
entry.getValue().getRenders().get(index)), lootContext)).orElse(0);
curio -> {
NonNullList<Boolean> renderStates = entry.getValue().getRenders();
return curio.getFortuneLevel(
new SlotContext(entry.getKey(), this.wearer, index, false,
renderStates.size() > index && renderStates.get(index)), lootContext);
}).orElse(0);
}
}
return fortuneLevel;
Expand All @@ -197,9 +200,13 @@ public int getLootingLevel(DamageSource source, LivingEntity target, int baseLoo
for (int i = 0; i < stacks.getSlots(); i++) {
int index = i;
lootingLevel += CuriosApi.getCuriosHelper().getCurio(stacks.getStackInSlot(i)).map(
curio -> curio.getLootingLevel(
new SlotContext(entry.getKey(), this.wearer, index, false,
entry.getValue().getRenders().get(index)), source, target, baseLooting))
curio -> {
NonNullList<Boolean> renderStates = entry.getValue().getRenders();
return curio.getLootingLevel(
new SlotContext(entry.getKey(), this.wearer, index, false,
renderStates.size() > index && renderStates.get(index)), source, target,
baseLooting);
})
.orElse(0);
}
}
Expand Down Expand Up @@ -411,8 +418,9 @@ public void readTag(Tag nbt) {
while (index < newStacksHandler.getSlots() && index < prevStacksHandler
.getSlots()) {
ItemStack prevStack = prevStacksHandler.getStacks().getStackInSlot(index);
NonNullList<Boolean> renderStates = newStacksHandler.getRenders();
SlotContext slotContext = new SlotContext(identifier, livingEntity, index, false,
newStacksHandler.getRenders().get(index));
renderStates.size() > index && renderStates.get(index));

if (!prevStack.isEmpty()) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,8 @@ private static void handleDrops(String identifier, LivingEntity livingEntity,
boolean keepInventory, LivingDropsEvent evt) {
for (int i = 0; i < stacks.getSlots(); i++) {
ItemStack stack = stacks.getStackInSlot(i);
SlotContext slotContext =
new SlotContext(identifier, livingEntity, i, cosmetic, renders.get(i));
SlotContext slotContext = new SlotContext(identifier, livingEntity, i, cosmetic,
renders.size() > i && renders.get(i));

if (!stack.isEmpty()) {
DropRule dropRuleOverride = null;
Expand Down Expand Up @@ -325,8 +325,9 @@ public void curioRightClick(PlayerInteractEvent.RightClickItem evt) {

for (int i = 0; i < stackHandler.getSlots(); i++) {
String id = entry.getKey();
SlotContext slotContext =
new SlotContext(id, player, i, false, entry.getValue().getRenders().get(i));
NonNullList<Boolean> renderStates = entry.getValue().getRenders();
SlotContext slotContext = new SlotContext(id, player, i, false,
renderStates.size() > i && renderStates.get(i));
CurioEquipEvent equipEvent = new CurioEquipEvent(stack, slotContext);
MinecraftForge.EVENT_BUS.post(equipEvent);
Event.Result result = equipEvent.getResult();
Expand Down Expand Up @@ -390,7 +391,8 @@ private static void replaceInvalidStacks(ICuriosHelper curiosHelper, ServerPlaye
NonNullList<Boolean> renders) {
for (int i = 0; i < stacks.getSlots(); i++) {
ItemStack stack = stacks.getStackInSlot(i);
SlotContext slotContext = new SlotContext(id, player, i, cosmetic, renders.get(i));
SlotContext slotContext =
new SlotContext(id, player, i, cosmetic, renders.size() > i && renders.get(i));

if (!stack.isEmpty() && !curiosHelper.isStackValid(slotContext, stack)) {
stacks.setStackInSlot(i, ItemStack.EMPTY);
Expand Down Expand Up @@ -421,8 +423,9 @@ public void onBreakBlock(BlockEvent.BreakEvent evt) {
IDynamicStackHandler stacks = entry.getValue().getStacks();

for (int i = 0; i < stacks.getSlots(); i++) {
NonNullList<Boolean> renderStates = entry.getValue().getRenders();
SlotContext slotContext = new SlotContext(entry.getKey(), player, i, false,
entry.getValue().getRenders().get(i));
renderStates.size() > i && renderStates.get(i));
fortuneLevel.addAndGet(curiosHelper.getCurio(stacks.getStackInSlot(i)).map(
curio -> curio.getFortuneLevel(slotContext, null))
.orElse(0));
Expand Down Expand Up @@ -453,9 +456,10 @@ public void tick(LivingEvent.LivingUpdateEvent evt) {
IDynamicStackHandler stackHandler = stacksHandler.getStacks();
IDynamicStackHandler cosmeticStackHandler = stacksHandler.getCosmeticStacks();

for (int i = 0; i < stackHandler.getSlots(); i++) {
for (int i = 0; i < stacksHandler.getSlots(); i++) {
NonNullList<Boolean> renderStates = stacksHandler.getRenders();
SlotContext slotContext = new SlotContext(identifier, livingEntity, i, false,
stacksHandler.getRenders().get(i));
renderStates.size() > i && renderStates.get(i));
ItemStack stack = stackHandler.getStackInSlot(i);
LazyOptional<ICurio> currentCurio = CuriosApi.getCuriosHelper().getCurio(stack);
final int index = i;
Expand All @@ -476,7 +480,7 @@ public void tick(LivingEvent.LivingUpdateEvent evt) {
if (!ItemStack.matches(stack, prevStack)) {
LazyOptional<ICurio> prevCurio = CuriosApi.getCuriosHelper().getCurio(prevStack);
syncCurios(livingEntity, stack, currentCurio, prevCurio, identifier, index, false,
stacksHandler.getRenders().get(index), HandlerType.EQUIPMENT);
renderStates.size() > index && renderStates.get(index), HandlerType.EQUIPMENT);
MinecraftForge.EVENT_BUS
.post(new CurioChangeEvent(livingEntity, identifier, i, prevStack, stack));
UUID uuid = UUID.nameUUIDFromBytes((identifier + i).getBytes());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@ public String getIdentifier() {
}

public boolean getRenderStatus() {
return this.renderStatuses.get(this.getSlotIndex());
return this.renderStatuses.size() > this.getSlotIndex() &&
this.renderStatuses.get(this.getSlotIndex());
}

@OnlyIn(Dist.CLIENT)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import java.util.Set;
import java.util.UUID;
import java.util.function.Supplier;
import net.minecraft.core.NonNullList;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.server.level.ServerPlayer;
Expand Down Expand Up @@ -62,8 +63,9 @@ public static void handle(CPacketDestroy msg, Supplier<NetworkEvent.Context> ctx

for (int i = 0; i < stackHandler.getSlots(); i++) {
UUID uuid = UUID.nameUUIDFromBytes((id + i).getBytes());
SlotContext slotContext =
new SlotContext(id, sender, i, false, stacksHandler.getRenders().get(i));
NonNullList<Boolean> renderStates = stacksHandler.getRenders();
SlotContext slotContext = new SlotContext(id, sender, i, false,
renderStates.size() > i && renderStates.get(i));
ItemStack stack = stackHandler.getStackInSlot(i);
Multimap<Attribute, AttributeModifier> map =
CuriosApi.getCuriosHelper().getAttributeModifiers(slotContext, uuid, stack);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.util.function.Supplier;
import net.minecraft.client.Minecraft;
import net.minecraft.client.multiplayer.ClientLevel;
import net.minecraft.core.NonNullList;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.LivingEntity;
Expand Down Expand Up @@ -66,9 +67,10 @@ public static void handle(SPacketBreak msg, Supplier<NetworkEvent.Context> ctx)
.ifPresent(handler -> handler.getStacksHandler(msg.curioId).ifPresent(stacks -> {
ItemStack stack = stacks.getStacks().getStackInSlot(msg.slotId);
LazyOptional<ICurio> possibleCurio = CuriosApi.getCuriosHelper().getCurio(stack);
NonNullList<Boolean> renderStates = stacks.getRenders();
possibleCurio.ifPresent(curio -> curio.curioBreak(
new SlotContext(msg.curioId, livingEntity, msg.slotId, false,
stacks.getRenders().get(msg.slotId))));
renderStates.size() > msg.slotId && renderStates.get(msg.slotId))));

if (!possibleCurio.isPresent()) {
ICurio.playBreakAnimation(stack, livingEntity);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.util.function.Supplier;
import net.minecraft.client.Minecraft;
import net.minecraft.client.multiplayer.ClientLevel;
import net.minecraft.core.NonNullList;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.world.entity.Entity;
Expand Down Expand Up @@ -80,9 +81,10 @@ public static void handle(SPacketSyncStack msg, Supplier<NetworkEvent.Context> c
boolean cosmetic = HandlerType.fromValue(msg.handlerType) == HandlerType.COSMETIC;

if (!compoundNBT.isEmpty()) {
NonNullList<Boolean> renderStates = stacksHandler.getRenders();
CuriosApi.getCuriosHelper().getCurio(stack).ifPresent(curio -> curio.readSyncData(
new SlotContext(msg.curioId, (LivingEntity) entity, slot, cosmetic,
stacksHandler.getRenders().get(slot)), compoundNBT));
renderStates.size() > slot && renderStates.get(slot)), compoundNBT));
}

if (cosmetic) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package top.theillusivec4.curios.mixin;

import java.util.Map;
import net.minecraft.core.NonNullList;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.monster.EnderMan;
Expand All @@ -22,10 +23,11 @@ public static boolean hasEnderMask(LivingEntity livingEntity, EnderMan enderMan)

for (int i = 0; i < stacks.getSlots(); i++) {
final int index = i;
NonNullList<Boolean> renderStates = entry.getValue().getRenders();
boolean hasMask =
CuriosApi.getCuriosHelper().getCurio(stacks.getStackInSlot(i)).map(curio -> curio
.isEnderMask(new SlotContext(entry.getKey(), livingEntity, index, false,
entry.getValue().getRenders().get(index)), enderMan))
renderStates.size() > index && renderStates.get(index)), enderMan))
.orElse(false);

if (hasMask) {
Expand All @@ -45,10 +47,11 @@ public static boolean canNeutralizePiglins(LivingEntity livingEntity) {

for (int i = 0; i < stacks.getSlots(); i++) {
final int index = i;
NonNullList<Boolean> renderStates = entry.getValue().getRenders();
boolean canNeutralize =
CuriosApi.getCuriosHelper().getCurio(stacks.getStackInSlot(i)).map(curio -> curio
.makesPiglinsNeutral(new SlotContext(entry.getKey(), livingEntity, index, false,
entry.getValue().getRenders().get(index))))
renderStates.size() > index && renderStates.get(index))))
.orElse(false);

if (canNeutralize) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.Wearable;
import net.minecraftforge.common.capabilities.ICapabilityProvider;
import top.theillusivec4.curios.api.CuriosApi;
import top.theillusivec4.curios.api.SlotContext;
import top.theillusivec4.curios.api.type.capability.ICurio;
import top.theillusivec4.curios.common.capability.CurioItemCapability;
Expand Down Expand Up @@ -80,6 +81,8 @@ public Multimap<Attribute, AttributeModifier> getAttributeModifiers(SlotContext
atts.put(Attributes.KNOCKBACK_RESISTANCE,
new AttributeModifier(uuid, CuriosTest.MODID + ":knockback_resist", 0.2,
AttributeModifier.Operation.ADDITION));
CuriosApi.getCuriosHelper()
.addSlotModifier(atts, "ring", uuid, 1, AttributeModifier.Operation.ADDITION);
return atts;
}

Expand Down

0 comments on commit 8a284e6

Please sign in to comment.