diff --git a/src/main/java/baubles/api/BaublesApi.java b/src/main/java/baubles/api/BaublesApi.java index 90cf690..ff04641 100644 --- a/src/main/java/baubles/api/BaublesApi.java +++ b/src/main/java/baubles/api/BaublesApi.java @@ -2,6 +2,7 @@ package baubles.api; import baubles.api.expanded.IBaubleSlotInventory; import baubles.api.expanded.IBaubleRenderInventory; +import baubles.api.expanded.IBaubleCosmeticInventory; import baubles.api.expanded.IBaubleSlotSettings; import baubles.api.expanded.BaubleSlotResult; import baubles.api.expanded.BaubleSlotContext; @@ -88,6 +89,27 @@ public class BaublesApi return !(inventory instanceof IBaubleRenderInventory renders) || renders.isSlotRendered(slot); } + /** + * Returns optional cosmetic storage without replacing the functional inventory. + */ + public static IInventory getBaubleCosmetics(EntityPlayer player) { + IInventory inventory = getBaubles(player); + return inventory instanceof IBaubleCosmeticInventory cosmetics ? cosmetics.getCosmeticInventory() : null; + } + + /** + * Selects the visible model: a cosmetic replacement, or the functional item when none is stored. + * The visibility toggle also hides cosmetic replacements. This is only a rendering query. + */ + public static ItemStack getRenderedBauble(IInventory inventory, int slot) { + if (inventory == null || slot < 0 || slot >= inventory.getSizeInventory() || !isBaubleRendered(inventory, slot)) return null; + if (inventory instanceof IBaubleCosmeticInventory cosmetics) { + ItemStack replacement = cosmetics.getCosmeticInventory().getStackInSlot(slot); + if (replacement != null) return replacement; + } + return inventory.getStackInSlot(slot); + } + /** * Retrieves the baubles inventory for the supplied player */ diff --git a/src/main/java/baubles/api/expanded/IBaubleCosmeticInventory.java b/src/main/java/baubles/api/expanded/IBaubleCosmeticInventory.java new file mode 100644 index 0000000..2e2e813 --- /dev/null +++ b/src/main/java/baubles/api/expanded/IBaubleCosmeticInventory.java @@ -0,0 +1,15 @@ +package baubles.api.expanded; + +import net.minecraft.inventory.IInventory; + +/** + * Optional cosmetic storage with the same slot indices as the functional inventory. + * Cosmetic items never provide equipment effects. Mutations belong on the server game thread. + */ +public interface IBaubleCosmeticInventory extends IInventory { + + /** + * Returns the persistent cosmetic inventory. Its contents only replace rendered models. + */ + IInventory getCosmeticInventory(); +} diff --git a/src/main/java/baubles/client/ClientProxy.java b/src/main/java/baubles/client/ClientProxy.java index 059788f..07c2f30 100644 --- a/src/main/java/baubles/client/ClientProxy.java +++ b/src/main/java/baubles/client/ClientProxy.java @@ -30,6 +30,7 @@ public class ClientProxy extends CommonProxy { if (world instanceof WorldClient) { switch (ID) { case Baubles.GUI: return new GuiPlayerExpanded(player); + case Baubles.GUI_COSMETICS: return new GuiPlayerExpanded(player, true); } } return null; diff --git a/src/main/java/baubles/client/gui/GuiBaubleCosmeticButton.java b/src/main/java/baubles/client/gui/GuiBaubleCosmeticButton.java new file mode 100644 index 0000000..4f473bd --- /dev/null +++ b/src/main/java/baubles/client/gui/GuiBaubleCosmeticButton.java @@ -0,0 +1,37 @@ +package baubles.client.gui; + +import net.minecraft.client.Minecraft; +import net.minecraft.client.gui.GuiButton; +import net.minecraft.client.resources.I18n; +import net.minecraft.util.ResourceLocation; +import org.lwjgl.opengl.GL11; + +public class GuiBaubleCosmeticButton extends GuiButton { + + private static final ResourceLocation OFF = new ResourceLocation("baubles", "textures/gui/cosmetic_off.png"); + private static final ResourceLocation OFF_HIGHLIGHTED = new ResourceLocation("baubles", "textures/gui/cosmetic_off_highlighted.png"); + private static final ResourceLocation ON = new ResourceLocation("baubles", "textures/gui/cosmetic_on.png"); + private static final ResourceLocation ON_HIGHLIGHTED = new ResourceLocation("baubles", "textures/gui/cosmetic_on_highlighted.png"); + private final boolean cosmeticView; + + public GuiBaubleCosmeticButton(int id, int x, int y, boolean cosmeticView) { + super(id, x, y, 20, 17, I18n.format("button.baubles.cosmetics")); + this.cosmeticView = cosmeticView; + } + + @Override + public void drawButton(Minecraft mc, int mouseX, int mouseY) { + if (!visible) return; + field_146123_n = mouseX >= xPosition && mouseY >= yPosition && mouseX < xPosition + width && mouseY < yPosition + height; + ResourceLocation texture = cosmeticView ? (field_146123_n ? ON_HIGHLIGHTED : ON) + : (field_146123_n ? OFF_HIGHLIGHTED : OFF); + GL11.glPushAttrib(GL11.GL_ENABLE_BIT | GL11.GL_COLOR_BUFFER_BIT | GL11.GL_TEXTURE_BIT | GL11.GL_CURRENT_BIT); + GL11.glEnable(GL11.GL_BLEND); + GL11.glEnable(GL11.GL_TEXTURE_2D); + GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA); + GL11.glColor4f(1, 1, 1, 1); + mc.getTextureManager().bindTexture(texture); + func_146110_a(xPosition, yPosition, 0, 0, width, height, width, height); + GL11.glPopAttrib(); + } +} diff --git a/src/main/java/baubles/client/gui/GuiEvents.java b/src/main/java/baubles/client/gui/GuiEvents.java index 71fe025..378bb53 100644 --- a/src/main/java/baubles/client/gui/GuiEvents.java +++ b/src/main/java/baubles/client/gui/GuiEvents.java @@ -79,7 +79,7 @@ public class GuiEvents { inventory.drawRenderToggleTooltip(event.mouseX, event.mouseY); } for (GuiButton button : GuiScreenReflectionHelper.getButtons(event.gui)) { - if ((button instanceof GuiBaublesButton) && button.visible && button.func_146115_a()) { + if ((button instanceof GuiBaublesButton || button instanceof GuiBaubleCosmeticButton) && button.visible && button.func_146115_a()) { GuiScreenReflectionHelper.drawTooltip(event.gui, Collections.singletonList(button.displayString), event.mouseX, event.mouseY); return; diff --git a/src/main/java/baubles/client/gui/GuiPlayerExpanded.java b/src/main/java/baubles/client/gui/GuiPlayerExpanded.java index 2f079d5..ee5bcdc 100644 --- a/src/main/java/baubles/client/gui/GuiPlayerExpanded.java +++ b/src/main/java/baubles/client/gui/GuiPlayerExpanded.java @@ -7,6 +7,7 @@ import baubles.common.container.ContainerPlayerExpanded; import baubles.common.container.SlotBauble; import baubles.common.network.PacketHandler; import baubles.common.network.PacketToggleBaubleRender; +import baubles.common.network.PacketToggleBaubleCosmetics; import codechicken.lib.vec.Rectangle4i; import codechicken.nei.NEIClientConfig; import codechicken.nei.VisiblityData; @@ -95,9 +96,14 @@ public class GuiPlayerExpanded extends GuiContainer implements INEIGuiHandler, I private final List tooltipCache = new ArrayList<>(2); private int renderToggleMouseButton = -1; private final Map slotIcons = new HashMap<>(); + private GuiBaubleCosmeticButton cosmeticButton; public GuiPlayerExpanded(EntityPlayer player) { - super(new ContainerPlayerExpanded(player.inventory, !player.worldObj.isRemote, player)); + this(player, false); + } + + public GuiPlayerExpanded(EntityPlayer player, boolean cosmeticView) { + super(new ContainerPlayerExpanded(player.inventory, !player.worldObj.isRemote, player, cosmeticView)); useOldGuiRendering = ((ContainerPlayerExpanded) inventorySlots).usesOldGuiRendering(); allowUserInput = true; } @@ -120,6 +126,11 @@ public class GuiPlayerExpanded extends GuiContainer implements INEIGuiHandler, I buttonList.clear(); renderToggleMouseButton = Mouse.isButtonDown(0) ? 0 : -1; super.initGui(); + cosmeticButton = new GuiBaubleCosmeticButton(56, guiLeft + (useOldGuiRendering ? 80 : -20), + guiTop + (useOldGuiRendering ? -18 : -14), + ((ContainerPlayerExpanded) inventorySlots).isCosmeticView()); + buttonList.add(cosmeticButton); + cosmeticButton.visible = ((ContainerPlayerExpanded) inventorySlots).baubles.hasCosmeticSlots(); if (!this.mc.thePlayer.getActivePotionEffects().isEmpty() && !useOldGuiRendering) { this.showActivePotionEffects = true; @@ -234,6 +245,7 @@ public class GuiPlayerExpanded extends GuiContainer implements INEIGuiHandler, I @Override protected void drawGuiContainerBackgroundLayer(float partialTicks, int mouseX, int mouseY) { GL11.glColor4f(1.0F, 1.0F, 1.0F, 1.0F); + drawCosmeticTabBackground(); if (useOldGuiRendering) { mc.getTextureManager().bindTexture(background); } else { @@ -241,6 +253,7 @@ public class GuiPlayerExpanded extends GuiContainer implements INEIGuiHandler, I } this.drawBaubleSlots(); + drawCosmeticSlotBackgrounds(); drawCustomSlotIcons(); if (showActivePotionEffects) { drawPotionEffects(); @@ -250,6 +263,33 @@ public class GuiPlayerExpanded extends GuiContainer implements INEIGuiHandler, I func_147046_a(guiLeft + 51, guiTop + 75, 30, (float) (guiLeft + 51) - xSizeFloat, (float) (guiTop + 25) - ySizeFloat, mc.thePlayer); } + private void drawCosmeticTabBackground() { + if (cosmeticButton == null || !cosmeticButton.visible) return; + GL11.glPushAttrib(GL11.GL_ENABLE_BIT | GL11.GL_COLOR_BUFFER_BIT | GL11.GL_TEXTURE_BIT); + GL11.glEnable(GL11.GL_BLEND); + GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA); + mc.getTextureManager().bindTexture(COSMETIC_INVENTORY_TEXTURE); + drawTexturedModalRect(cosmeticButton.xPosition - 4, cosmeticButton.yPosition - 5, 32, 0, 28, 24); + GL11.glPopAttrib(); + } + + private void drawCosmeticSlotBackgrounds() { + ContainerPlayerExpanded container = (ContainerPlayerExpanded) inventorySlots; + if (!container.isCosmeticView()) return; + GL11.glPushAttrib(GL11.GL_ENABLE_BIT | GL11.GL_COLOR_BUFFER_BIT | GL11.GL_TEXTURE_BIT); + GL11.glDisable(GL11.GL_ALPHA_TEST); + GL11.glEnable(GL11.GL_BLEND); + GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA); + mc.getTextureManager().bindTexture(COSMETIC_INVENTORY_TEXTURE); + for (int i = 0; i < container.getBaubleSlotCount(); i++) { + SlotBauble slot = container.getBaubleSlot(i); + if (!isVisibleBaubleSlot(slot) || !slot.isCosmeticSlot()) continue; + drawTexturedModalRect(guiLeft + slot.xDisplayPosition - 1, guiTop + slot.yDisplayPosition - 1, + 32, 50, 18, 18); + } + GL11.glPopAttrib(); + } + private void drawCustomSlotIcons() { ContainerPlayerExpanded container = (ContainerPlayerExpanded) inventorySlots; for (int i = 0; i < container.getBaubleSlotCount(); i++) { @@ -452,6 +492,9 @@ public class GuiPlayerExpanded extends GuiContainer implements INEIGuiHandler, I // Strip formatting codes String strippedType = StatCollector.translateToLocal("slot." + slotType).replaceAll("§[0-9a-fklmnor]", ""); tooltipCache.add(strippedType); + if (slot.isCosmeticSlot()) { + tooltipCache.add(EnumChatFormatting.LIGHT_PURPLE + StatCollector.translateToLocal("tooltip.baubles.cosmetic")); + } if (!slot.isSlotActive()) tooltipCache.add(EnumChatFormatting.GRAY + StatCollector.translateToLocal("tooltip.baubles.inactive")); ItemStack heldItem = mc.thePlayer.inventory.getItemStack(); @@ -540,6 +583,11 @@ public class GuiPlayerExpanded extends GuiContainer implements INEIGuiHandler, I @Override protected void actionPerformed(GuiButton button) { + if (button instanceof GuiBaubleCosmeticButton) { + ContainerPlayerExpanded container = (ContainerPlayerExpanded) inventorySlots; + PacketHandler.INSTANCE.sendToServer(new PacketToggleBaubleCosmetics(container.windowId, container.baubles.getLayoutRevision())); + return; + } if (button.id == 0) { mc.displayGuiScreen(new GuiAchievements(this, mc.thePlayer.getStatFileWriter())); } else if (button.id == 1) { @@ -566,6 +614,13 @@ public class GuiPlayerExpanded extends GuiContainer implements INEIGuiHandler, I @Override protected void mouseClicked(int mouseX, int mouseY, int mouseButton) { + for (Object value : buttonList) { + if (value instanceof GuiBaubleCosmeticButton button && button.mousePressed(mc, mouseX, mouseY)) { + renderToggleMouseButton = mouseButton; + if (mouseButton == 0) actionPerformed(button); + return; + } + } SlotBauble toggle = getRenderToggleAt(mouseX, mouseY); if (toggle != null) { renderToggleMouseButton = mouseButton; diff --git a/src/main/java/baubles/common/Baubles.java b/src/main/java/baubles/common/Baubles.java index dfa1201..37e3fab 100644 --- a/src/main/java/baubles/common/Baubles.java +++ b/src/main/java/baubles/common/Baubles.java @@ -47,6 +47,7 @@ public class Baubles { public static final Logger log = LogManager.getLogger("Baubles"); public static final int GUI = 0; + public static final int GUI_COSMETICS = 1; public static final Item itemDebugger = new ItemDebugger().setUnlocalizedName("baubleSlotDebugTool"); diff --git a/src/main/java/baubles/common/CommonProxy.java b/src/main/java/baubles/common/CommonProxy.java index 9436822..476811b 100644 --- a/src/main/java/baubles/common/CommonProxy.java +++ b/src/main/java/baubles/common/CommonProxy.java @@ -23,8 +23,8 @@ public class CommonProxy implements IGuiHandler { @Override public Object getServerGuiElement(int ID, EntityPlayer player, World world, int x, int y, int z) { - if(ID == Baubles.GUI) { - return new ContainerPlayerExpanded(player.inventory, !world.isRemote, player); + if(ID == Baubles.GUI || ID == Baubles.GUI_COSMETICS) { + return new ContainerPlayerExpanded(player.inventory, !world.isRemote, player, ID == Baubles.GUI_COSMETICS); } return null; } diff --git a/src/main/java/baubles/common/container/ContainerPlayerExpanded.java b/src/main/java/baubles/common/container/ContainerPlayerExpanded.java index b2ea332..841cc9e 100644 --- a/src/main/java/baubles/common/container/ContainerPlayerExpanded.java +++ b/src/main/java/baubles/common/container/ContainerPlayerExpanded.java @@ -36,6 +36,7 @@ public class ContainerPlayerExpanded extends Container { private final EntityPlayer thePlayer; private final boolean useOldGuiRendering; private final long layoutRevision; + private final boolean cosmeticView; private final List visibleBaubleSlots = new ArrayList<>(); private int slotsAdded = 0; @@ -44,8 +45,13 @@ public class ContainerPlayerExpanded extends Container { private int baubleSlotCount = 0; public ContainerPlayerExpanded(InventoryPlayer playerInv, boolean isClient, EntityPlayer player) { + this(playerInv, isClient, player, false); + } + + public ContainerPlayerExpanded(InventoryPlayer playerInv, boolean isClient, EntityPlayer player, boolean cosmeticView) { this.thePlayer = player; baubles = PlayerHandler.getPlayerBaubles(player); + this.cosmeticView = cosmeticView && baubles.hasCosmeticSlots(); useOldGuiRendering = baubles.usesOldGuiRendering(); layoutRevision = baubles.getLayoutRevision(); baubles.setEventHandler(this); @@ -101,7 +107,9 @@ public class ContainerPlayerExpanded extends Container { boolean visible = isTypeVisible(type); int x = useOldGuiRendering ? slotStartX + slotOffset * (visibleIndex / 4) : -18 - 18 * (visibleIndex % getColumns()); int y = useOldGuiRendering ? slotStartY + slotOffset * (visibleIndex % 4) : 12 + slotOffset * (visibleIndex / getColumns()); - SlotBauble slot = new SlotBauble(baubles, type, index, x, y); + SlotBauble slot = this.cosmeticView && baubles.getSlotDefinition(type).cosmetic() + ? new SlotBaubleCosmetic(baubles.getCosmeticInventory(), type, index, x, y) + : new SlotBauble(baubles, type, index, x, y); addSlotToContainer(slot); if (visible) { visibleBaubleSlots.add(slot); @@ -183,6 +191,10 @@ public class ContainerPlayerExpanded extends Container { return useOldGuiRendering; } + public boolean isCosmeticView() { + return cosmeticView; + } + private boolean isTypeVisible(String type) { return baubles.getSlotDefinition(type).nativeGui() && (BaublesConfig.showUnusedSlots || !BaubleExpandedSlots.unknownType.equals(type)); @@ -285,13 +297,13 @@ public class ContainerPlayerExpanded extends Container { ItemStack removed = slot.decrStackSize(originalStack.stackSize - remaining.stackSize); slot.onPickupFromSlot(player, removed); return returnStack; - } else if (item instanceof ItemArmor armor + } else if (!cosmeticView && item instanceof ItemArmor armor && !((Slot) inventorySlots.get(craftingActive + armor.armorType)).getHasStack()) { int armorSlot = craftingActive + armor.armorType; if (!mergeItemStack(originalStack, armorSlot, armorSlot + 1, false)) { returnStack = null; } - } else if (item instanceof IBauble bauble && bauble.canEquip(returnStack, thePlayer)) { + } else if (item instanceof IBauble bauble && (cosmeticView || bauble.canEquip(returnStack, thePlayer))) { String[] types = item instanceof IBaubleExpanded ? ((IBaubleExpanded) item).getBaubleTypes(returnStack) : new String[] {BaubleExpandedSlots.getTypeFromBaubleType(bauble.getBaubleType(returnStack))}; diff --git a/src/main/java/baubles/common/container/InventoryBaubleCosmetics.java b/src/main/java/baubles/common/container/InventoryBaubleCosmetics.java new file mode 100644 index 0000000..c7cd610 --- /dev/null +++ b/src/main/java/baubles/common/container/InventoryBaubleCosmetics.java @@ -0,0 +1,191 @@ +package baubles.common.container; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.function.Consumer; + +import baubles.api.expanded.BaubleExpandedSlots; +import baubles.common.lib.ItemStackHelper; +import baubles.common.network.PacketHandler; +import baubles.common.network.PacketSyncBaubleCosmetic; +import net.minecraft.entity.Entity; +import net.minecraft.entity.item.EntityItem; +import net.minecraft.entity.player.EntityPlayer; +import net.minecraft.entity.player.EntityPlayerMP; +import net.minecraft.inventory.IInventory; +import net.minecraft.item.ItemStack; +import net.minecraft.nbt.NBTTagCompound; +import net.minecraft.nbt.NBTTagList; + +public class InventoryBaubleCosmetics implements IInventory { + + private final InventoryBaubles owner; + private ItemStack[] stacks; + private ItemStack[] previousStacks = new ItemStack[0]; + + public InventoryBaubleCosmetics(InventoryBaubles owner) { + this.owner = owner; + stacks = new ItemStack[owner.stackList.length]; + } + + @Override + public int getSizeInventory() { + return stacks.length; + } + + @Override + public ItemStack getStackInSlot(int slot) { + return slot >= 0 && slot < stacks.length ? stacks[slot] : null; + } + + @Override + public ItemStack decrStackSize(int slot, int amount) { + ItemStack current = getStackInSlot(slot); + if (current == null || amount <= 0) return null; + ItemStack taken = current.stackSize <= amount ? current : current.splitStack(amount); + if (taken == current || current.stackSize == 0) stacks[slot] = null; + markDirty(); + syncSlot(slot); + return taken; + } + + @Override + public ItemStack getStackInSlotOnClosing(int slot) { + ItemStack stack = getStackInSlot(slot); + if (stack != null) setInventorySlotContents(slot, null); + return stack; + } + + @Override + public void setInventorySlotContents(int slot, ItemStack stack) { + if (slot < 0 || slot >= stacks.length) return; + stacks[slot] = stack; + markDirty(); + syncSlot(slot); + } + + @Override + public String getInventoryName() { + return "container.baubles.cosmetics"; + } + + @Override + public boolean hasCustomInventoryName() { + return false; + } + + @Override + public int getInventoryStackLimit() { + return 1; + } + + @Override + public void markDirty() { + owner.markDirty(); + } + + @Override + public boolean isUseableByPlayer(EntityPlayer player) { + return owner.player.get() == player; + } + + @Override + public void openInventory() {} + + @Override + public void closeInventory() {} + + @Override + public boolean isItemValidForSlot(int slot, ItemStack stack) { + String type = owner.getSlotType(slot); + return owner.isSlotActive(slot) && owner.getSlotDefinition(type).cosmetic() + && owner.matchesSlotType(slot, stack) && BaubleLifecycle.canEquip(stack, owner.getSlotContext(slot, true)); + } + + public void syncSlot(int slot) { + if (previousStacks.length != stacks.length) previousStacks = Arrays.copyOf(previousStacks, stacks.length); + if (slot >= 0 && slot < stacks.length) previousStacks[slot] = ItemStack.copyItemStack(stacks[slot]); + EntityPlayer player = owner.player.get(); + if (player instanceof EntityPlayerMP && !player.worldObj.isRemote) { + PacketHandler.sendToTracking(new PacketSyncBaubleCosmetic(player, slot), player, true); + } + } + + public void detectChanges() { + if (previousStacks.length != stacks.length) previousStacks = Arrays.copyOf(previousStacks, stacks.length); + for (int slot = 0; slot < stacks.length; slot++) { + if (!ItemStack.areItemStacksEqual(previousStacks[slot], stacks[slot])) syncSlot(slot); + } + } + + public InventoryBaubles getOwner() { + return owner; + } + + public void resize(BaubleSlotState previous, Consumer removed) { + ItemStack[] old = stacks; + stacks = Arrays.copyOf(old, owner.getSizeInventory()); + for (int slot = 0; slot < old.length; slot++) { + if (slot < stacks.length && previous.getType(slot).equals(owner.getSlotType(slot)) + && owner.getSlotDefinition(owner.getSlotType(slot)).cosmetic() + && (old[slot] == null || owner.matchesSlotType(slot, old[slot]))) continue; + if (slot < stacks.length) stacks[slot] = null; + if (old[slot] != null) removed.accept(old[slot]); + } + previousStacks = copyStacks(); + } + + public ItemStack[] copyStacks() { + ItemStack[] copy = new ItemStack[stacks.length]; + for (int slot = 0; slot < copy.length; slot++) copy[slot] = ItemStack.copyItemStack(stacks[slot]); + return copy; + } + + public void applySnapshot(ItemStack[] snapshot) { + if (snapshot.length != owner.getSizeInventory()) throw new IllegalArgumentException("Cosmetic snapshot size differs"); + stacks = snapshot.clone(); + previousStacks = copyStacks(); + } + + public void writeNBT(NBTTagCompound tags) { + NBTTagList entries = new NBTTagList(); + for (int slot = 0; slot < stacks.length; slot++) { + if (stacks[slot] == null) continue; + NBTTagCompound entry = stacks[slot].writeToNBT(new NBTTagCompound()); + entry.setInteger("SlotIndex", slot); + entries.appendTag(entry); + } + tags.setTag("Baubles.Cosmetics", entries); + } + + public void readNBT(NBTTagCompound tags, BaubleSlotState saved, Consumer removed) { + stacks = new ItemStack[owner.getSizeInventory()]; + NBTTagList entries = tags.getTagList("Baubles.Cosmetics", 10); + for (int i = 0; i < entries.tagCount(); i++) { + NBTTagCompound entry = entries.getCompoundTagAt(i); + int slot = entry.getInteger("SlotIndex"); + ItemStack stack = ItemStack.loadItemStackFromNBT(entry); + if (stack == null) continue; + if (slot >= 0 && slot < stacks.length && stacks[slot] == null + && !BaubleExpandedSlots.unknownType.equals(owner.getSlotType(slot)) + && owner.getSlotDefinition(owner.getSlotType(slot)).cosmetic() + && owner.matchesSlotType(slot, stack) + && saved.getType(slot).equals(owner.getSlotType(slot))) stacks[slot] = stack; + else removed.accept(stack); + } + previousStacks = copyStacks(); + } + + public void dropItems(ArrayList drops, Entity entity, boolean keepSoulBound) { + for (int slot = 0; slot < stacks.length; slot++) { + ItemStack stack = stacks[slot]; + if (stack == null || keepSoulBound && ItemStackHelper.isSoulBound(stack)) continue; + EntityItem drop = new EntityItem(entity.worldObj, entity.posX, entity.posY + entity.getEyeHeight(), + entity.posZ, stack.copy()); + drop.delayBeforeCanPickup = 40; + drops.add(drop); + stacks[slot] = null; + syncSlot(slot); + } + } +} diff --git a/src/main/java/baubles/common/container/InventoryBaubles.java b/src/main/java/baubles/common/container/InventoryBaubles.java index 95f3633..7178e2c 100644 --- a/src/main/java/baubles/common/container/InventoryBaubles.java +++ b/src/main/java/baubles/common/container/InventoryBaubles.java @@ -6,6 +6,7 @@ import baubles.api.expanded.BaubleExpandedSlots; import baubles.api.expanded.IBaubleExpanded; import baubles.api.expanded.IBaubleSlotInventory; import baubles.api.expanded.IBaubleRenderInventory; +import baubles.api.expanded.IBaubleCosmeticInventory; import baubles.api.expanded.IBaubleSlotSettings; import baubles.api.expanded.IBaubleLifecycle; import baubles.api.expanded.BaubleSlotContext; @@ -40,7 +41,7 @@ import java.util.BitSet; import java.util.Collections; import java.util.Map; -public class InventoryBaubles implements IBaubleSlotInventory, IBaubleRenderInventory, IBaubleSlotSettings { +public class InventoryBaubles implements IBaubleSlotInventory, IBaubleRenderInventory, IBaubleCosmeticInventory, IBaubleSlotSettings { public ItemStack[] stackList; private Container eventHandler; public WeakReference player; @@ -55,12 +56,14 @@ public class InventoryBaubles implements IBaubleSlotInventory, IBaubleRenderInve private WeakReference loadedPlayer = new WeakReference<>(null); private final ArrayDeque pendingReturns = new ArrayDeque<>(); private final BitSet hiddenSlots = new BitSet(); + private final InventoryBaubleCosmetics cosmetics; private ItemStack[] previousStacks = new ItemStack[0]; public InventoryBaubles(EntityPlayer player) { slotState = new BaubleSlotState(BaubleExpandedSlots.getCurrentSlotAssignments(), Collections.emptyMap()); stackList = new ItemStack[slotState.getAssignments().length]; this.player = new WeakReference<>(player); + cosmetics = new InventoryBaubleCosmetics(this); } @Override @@ -125,6 +128,7 @@ public class InventoryBaubles implements IBaubleSlotInventory, IBaubleRenderInve } if (state.getAssignments().length != stackList.length) throw new IllegalArgumentException("Container slot count differs"); slotState = state; + cosmetics.applySnapshot(new ItemStack[stackList.length]); oldGuiRendering = data.getBoolean("OldGui"); hiddenSlots.clear(); for (int slot : data.getIntArray("HiddenSlots")) { @@ -141,6 +145,11 @@ public class InventoryBaubles implements IBaubleSlotInventory, IBaubleRenderInve return oldGuiRendering; } + @Override + public InventoryBaubleCosmetics getCosmeticInventory() { + return cosmetics; + } + @Override public boolean isSlotRendered(int slot) { return isSlotActive(slot) && (!getSlotDefinition(getSlotType(slot)).renderToggle() || !hiddenSlots.get(slot)); @@ -206,6 +215,14 @@ public class InventoryBaubles implements IBaubleSlotInventory, IBaubleRenderInve return isSlotActive(slot) && getSlotDefinition(getSlotType(slot)).renderToggle(); } + public boolean hasCosmeticSlots() { + for (String type : getSlotAssignments()) { + BaubleSlotType definition = getSlotDefinition(type); + if (!BaubleExpandedSlots.unknownType.equals(type) && definition.nativeGui() && definition.cosmetic()) return true; + } + return false; + } + public boolean canTypeFitSlot(String itemType, String slotType) { if (itemType == null || slotType == null) return false; return itemType.equals(slotType) || itemType.equals(BaubleExpandedSlots.universalType) @@ -297,6 +314,7 @@ public class InventoryBaubles implements IBaubleSlotInventory, IBaubleRenderInve slotState = updated; stackList = Arrays.copyOf(previous, assignments.length); layoutRevision++; + cosmetics.resize(previousState, pendingReturns::add); for (int slot = 0; slot < previous.length; slot++) { boolean retained = previousState.getType(slot).equals(updated.getType(slot)) && slot < stackList.length; if (retained) { @@ -386,6 +404,7 @@ public class InventoryBaubles implements IBaubleSlotInventory, IBaubleRenderInve } if (!owner.worldObj.isRemote) { detectStackChanges(); + cosmetics.detectChanges(); } } @@ -470,10 +489,14 @@ public class InventoryBaubles implements IBaubleSlotInventory, IBaubleRenderInve } public void applySnapshot(BaubleSlotState state, long revision, ItemStack[] stacks, boolean oldGui, int[] hidden) { + applySnapshot(state, revision, stacks, oldGui, hidden, new ItemStack[stacks.length]); + } + + public void applySnapshot(BaubleSlotState state, long revision, ItemStack[] stacks, boolean oldGui, int[] hidden, ItemStack[] cosmeticStacks) { EntityPlayer owner = player.get(); if (owner == null || !owner.worldObj.isRemote) return; if (loadedPlayer.get() == owner && revision < layoutRevision) return; - if (stacks.length != state.getAssignments().length) { + if (stacks.length != state.getAssignments().length || cosmeticStacks.length != stacks.length) { throw new IllegalArgumentException("Snapshot items and layout have different sizes"); } ItemStack[] previous = stackList; @@ -486,6 +509,7 @@ public class InventoryBaubles implements IBaubleSlotInventory, IBaubleRenderInve stackList = stacks.clone(); layoutRevision = revision; oldGuiRendering = oldGui; + cosmetics.applySnapshot(cosmeticStacks); hiddenSlots.clear(); for (int slot : hidden) { if (isValidSlot(slot) && getSlotDefinition(getSlotType(slot)).renderToggle()) hiddenSlots.set(slot); @@ -740,6 +764,7 @@ public class InventoryBaubles implements IBaubleSlotInventory, IBaubleRenderInve BaubleSlotStorage.write(tags, new BaubleSlotState(slotState.getAssignments(), getPendingSlotState().getModifiers(), slotState.getDefinitions(), getPendingSlotState().getInactiveSlots())); tags.setIntArray("Baubles.HiddenSlots", getHiddenSlots()); + cosmetics.writeNBT(tags); NBTTagList tagList = new NBTTagList(); NBTTagCompound invSlot; for (int slot = 0; slot < stackList.length; ++slot) { @@ -769,6 +794,7 @@ public class InventoryBaubles implements IBaubleSlotInventory, IBaubleRenderInve BaubleSlotConfiguration.getDefinitions()); stackList = new ItemStack[slotState.getAssignments().length]; pendingReturns.clear(); + cosmetics.readNBT(tags, saved, pendingReturns::add); hiddenSlots.clear(); for (int slot : tags.getIntArray("Baubles.HiddenSlots")) { if (isValidSlot(slot) && getSlotDefinition(getSlotType(slot)).renderToggle() @@ -801,6 +827,7 @@ public class InventoryBaubles implements IBaubleSlotInventory, IBaubleRenderInve } public void dropItems(ArrayList drops) { + cosmetics.dropItems(drops, player.get(), false); for (int slot = 0; slot < stackList.length; ++slot) { if (stackList[slot] != null) { EntityItem item = new EntityItem(player.get().worldObj, @@ -821,6 +848,7 @@ public class InventoryBaubles implements IBaubleSlotInventory, IBaubleRenderInve } public void dropItemsAt(ArrayList drops, Entity entity) { + cosmetics.dropItems(drops, entity, true); for (int slot = 0; slot < stackList.length; ++slot) { if (stackList[slot] != null && !ItemStackHelper.isSoulBound(stackList[slot])) { EntityItem item = new EntityItem(entity.worldObj, diff --git a/src/main/java/baubles/common/container/SlotBauble.java b/src/main/java/baubles/common/container/SlotBauble.java index 25797cd..de766bc 100644 --- a/src/main/java/baubles/common/container/SlotBauble.java +++ b/src/main/java/baubles/common/container/SlotBauble.java @@ -41,6 +41,7 @@ public class SlotBauble extends Slot { } public InventoryBaubles getBaublesInventory() { + if (inventory instanceof InventoryBaubleCosmetics cosmetics) return cosmetics.getOwner(); return inventory instanceof InventoryBaubles baubles ? baubles : null; } @@ -51,7 +52,7 @@ public class SlotBauble extends Slot { } public boolean isCosmeticSlot() { - return false; + return inventory instanceof InventoryBaubleCosmetics; } public boolean isSlotActive() { diff --git a/src/main/java/baubles/common/container/SlotBaubleCosmetic.java b/src/main/java/baubles/common/container/SlotBaubleCosmetic.java new file mode 100644 index 0000000..e2d021b --- /dev/null +++ b/src/main/java/baubles/common/container/SlotBaubleCosmetic.java @@ -0,0 +1,32 @@ +package baubles.common.container; + +import net.minecraft.entity.player.EntityPlayer; +import net.minecraft.item.ItemStack; + +public class SlotBaubleCosmetic extends SlotBauble { + + public SlotBaubleCosmetic(InventoryBaubleCosmetics inventory, String type, int slot, int x, int y) { + super(inventory, type, slot, x, y); + } + + @Override + public boolean isItemValid(ItemStack stack) { + return inventory.isItemValidForSlot(getSlotIndex(), stack); + } + + @Override + public boolean canTakeStack(EntityPlayer player) { + return super.canTakeStack(player); + } + + @Override + public int getSlotStackLimit() { + return 1; + } + + @Override + public void onSlotChanged() { + inventory.markDirty(); + ((InventoryBaubleCosmetics) inventory).syncSlot(getSlotIndex()); + } +} diff --git a/src/main/java/baubles/common/network/PacketHandler.java b/src/main/java/baubles/common/network/PacketHandler.java index 28d01fe..5044297 100644 --- a/src/main/java/baubles/common/network/PacketHandler.java +++ b/src/main/java/baubles/common/network/PacketHandler.java @@ -20,6 +20,8 @@ public class PacketHandler { INSTANCE.registerMessage(PacketSyncAllBauble.class, PacketSyncAllBauble.class, 3, Side.CLIENT); INSTANCE.registerMessage(PacketToggleBaubleRender.class, PacketToggleBaubleRender.class, 4, Side.SERVER); INSTANCE.registerMessage(PacketSyncBaubleRender.class, PacketSyncBaubleRender.class, 5, Side.CLIENT); + INSTANCE.registerMessage(PacketToggleBaubleCosmetics.class, PacketToggleBaubleCosmetics.class, 6, Side.SERVER); + INSTANCE.registerMessage(PacketSyncBaubleCosmetic.class, PacketSyncBaubleCosmetic.class, 7, Side.CLIENT); } public static void sendToTracking(IMessage message, EntityPlayer target, boolean sendToTarget) { diff --git a/src/main/java/baubles/common/network/PacketSyncAllBauble.java b/src/main/java/baubles/common/network/PacketSyncAllBauble.java index 4083b1d..4a7cee5 100644 --- a/src/main/java/baubles/common/network/PacketSyncAllBauble.java +++ b/src/main/java/baubles/common/network/PacketSyncAllBauble.java @@ -24,6 +24,7 @@ public class PacketSyncAllBauble implements IMessage, IMessageHandler { + + private int slot; + private int playerId; + private int dimension; + private long revision; + private ItemStack bauble; + + public PacketSyncBaubleCosmetic() {} + + public PacketSyncBaubleCosmetic(EntityPlayer player, int slot) { + InventoryBaubles inventory = PlayerHandler.getPlayerBaubles(player); + this.slot = slot; + bauble = ItemStack.copyItemStack(inventory.getCosmeticInventory().getStackInSlot(slot)); + playerId = player.getEntityId(); + dimension = player.dimension; + revision = inventory.getLayoutRevision(); + } + + @Override + public void toBytes(ByteBuf buffer) { + ByteBufUtils.writeVarInt(buffer, slot, 5); + buffer.writeInt(playerId); + buffer.writeInt(dimension); + buffer.writeLong(revision); + ByteBufUtils.writeItemStack(buffer, bauble); + } + + @Override + public void fromBytes(ByteBuf buffer) { + slot = BaubleSlotCodec.readSize(buffer, BaubleSlotState.MAX_SLOTS - 1); + playerId = buffer.readInt(); + dimension = buffer.readInt(); + revision = buffer.readLong(); + bauble = ByteBufUtils.readItemStack(buffer); + } + + @Override + public IMessage onMessage(PacketSyncBaubleCosmetic message, MessageContext ctx) { + World world = Baubles.proxy.getClientWorld(); + if (world == null || world.provider.dimensionId != message.dimension) return null; + Entity entity = world.getEntityByID(message.playerId); + if (entity instanceof EntityPlayer player) { + InventoryBaubles inventory = PlayerHandler.getPlayerBaubles(player); + if (message.revision == inventory.getLayoutRevision() && message.slot < inventory.getSizeInventory()) { + inventory.getCosmeticInventory().setInventorySlotContents(message.slot, message.bauble); + } + } + return null; + } +} diff --git a/src/main/java/baubles/common/network/PacketToggleBaubleCosmetics.java b/src/main/java/baubles/common/network/PacketToggleBaubleCosmetics.java new file mode 100644 index 0000000..e5fb383 --- /dev/null +++ b/src/main/java/baubles/common/network/PacketToggleBaubleCosmetics.java @@ -0,0 +1,59 @@ +package baubles.common.network; + +import baubles.common.Baubles; +import baubles.common.container.ContainerPlayerExpanded; +import cpw.mods.fml.common.network.simpleimpl.IMessage; +import cpw.mods.fml.common.network.simpleimpl.IMessageHandler; +import cpw.mods.fml.common.network.simpleimpl.MessageContext; +import io.netty.buffer.ByteBuf; +import net.minecraft.entity.player.EntityPlayerMP; +import net.minecraft.item.ItemStack; + +public class PacketToggleBaubleCosmetics implements IMessage, IMessageHandler { + + private int windowId; + private long revision; + + public PacketToggleBaubleCosmetics() {} + + public PacketToggleBaubleCosmetics(int windowId, long revision) { + this.windowId = windowId; + this.revision = revision; + } + + @Override + public void toBytes(ByteBuf buffer) { + buffer.writeInt(windowId); + buffer.writeLong(revision); + } + + @Override + public void fromBytes(ByteBuf buffer) { + windowId = buffer.readInt(); + revision = buffer.readLong(); + } + + @Override + public IMessage onMessage(PacketToggleBaubleCosmetics message, MessageContext ctx) { + EntityPlayerMP player = ctx.getServerHandler().playerEntity; + if (!(player.openContainer instanceof ContainerPlayerExpanded container) + || container.windowId != message.windowId || !container.canInteractWith(player) + || container.baubles.getLayoutRevision() != message.revision + || !container.baubles.hasCosmeticSlots()) return null; + int gui = container.isCosmeticView() ? Baubles.GUI : Baubles.GUI_COSMETICS; + PacketHandler.INSTANCE.sendTo(new PacketSyncAllBauble(player), player); + ItemStack cursor = player.inventory.getItemStack(); + player.inventory.setItemStack(null); + ItemStack[] crafting = new ItemStack[container.craftMatrix.getSizeInventory()]; + for (int slot = 0; slot < crafting.length; slot++) crafting[slot] = container.craftMatrix.getStackInSlotOnClosing(slot); + try { + player.openGui(Baubles.instance, gui, player.worldObj, (int) player.posX, (int) player.posY, (int) player.posZ); + } finally { + ContainerPlayerExpanded target = player.openContainer instanceof ContainerPlayerExpanded expanded ? expanded : container; + for (int slot = 0; slot < crafting.length; slot++) target.craftMatrix.setInventorySlotContents(slot, crafting[slot]); + player.inventory.setItemStack(cursor); + } + player.sendContainerToPlayer(player.openContainer); + return null; + } +} diff --git a/src/main/resources/assets/baubles/lang/en_US.lang b/src/main/resources/assets/baubles/lang/en_US.lang index d2f161e..f73ddb5 100644 --- a/src/main/resources/assets/baubles/lang/en_US.lang +++ b/src/main/resources/assets/baubles/lang/en_US.lang @@ -43,7 +43,11 @@ commands.baubles.type=Unknown bauble slot type: %s commands.baubles.unavailable=Dynamic bauble slots are not available for this player button.baubles.hide=Hide bauble model (currently visible) button.baubles.show=Show bauble model (currently hidden) +button.baubles.cosmetics=Toggle Cosmetics button.baubles.visibility=Toggle Visibility +button.baubles.equipment=Show equipment slots +container.baubles.cosmetics=Cosmetic baubles +tooltip.baubles.cosmetic=Cosmetic tooltip.baubles.inactive=Inactive slot commands.baubles.reloaded=Reloaded bauble slot configuration commands.baubles.active=Set %s %s slots active: %s diff --git a/src/main/resources/assets/baubles/lang/zh_CN.lang b/src/main/resources/assets/baubles/lang/zh_CN.lang index a4caa0f..7e9f858 100644 --- a/src/main/resources/assets/baubles/lang/zh_CN.lang +++ b/src/main/resources/assets/baubles/lang/zh_CN.lang @@ -40,7 +40,11 @@ commands.baubles.type=未知的饰品槽位类型:%s commands.baubles.unavailable=此玩家的饰品背包不支持动态槽位 button.baubles.hide=隐藏饰品模型(当前显示) button.baubles.show=显示饰品模型(当前隐藏) +button.baubles.cosmetics=切换装饰品 button.baubles.visibility=切换是否可见 +button.baubles.equipment=切换到功能槽 +container.baubles.cosmetics=饰品装饰栏 +tooltip.baubles.cosmetic=装饰品 tooltip.baubles.inactive=已停用的槽位 commands.baubles.reloaded=已重新加载饰品槽位配置 commands.baubles.active=已设置 %s 的 %s 槽位启用状态为 %s diff --git a/src/main/resources/assets/baubles/textures/gui/cosmetic_off.png b/src/main/resources/assets/baubles/textures/gui/cosmetic_off.png new file mode 100644 index 0000000..5b64601 Binary files /dev/null and b/src/main/resources/assets/baubles/textures/gui/cosmetic_off.png differ diff --git a/src/main/resources/assets/baubles/textures/gui/cosmetic_off_highlighted.png b/src/main/resources/assets/baubles/textures/gui/cosmetic_off_highlighted.png new file mode 100644 index 0000000..1b76238 Binary files /dev/null and b/src/main/resources/assets/baubles/textures/gui/cosmetic_off_highlighted.png differ diff --git a/src/main/resources/assets/baubles/textures/gui/cosmetic_on.png b/src/main/resources/assets/baubles/textures/gui/cosmetic_on.png new file mode 100644 index 0000000..861179e Binary files /dev/null and b/src/main/resources/assets/baubles/textures/gui/cosmetic_on.png differ diff --git a/src/main/resources/assets/baubles/textures/gui/cosmetic_on_highlighted.png b/src/main/resources/assets/baubles/textures/gui/cosmetic_on_highlighted.png new file mode 100644 index 0000000..3ec1c9f Binary files /dev/null and b/src/main/resources/assets/baubles/textures/gui/cosmetic_on_highlighted.png differ