diff --git a/src/main/java/baubles/client/gui/GuiEvents.java b/src/main/java/baubles/client/gui/GuiEvents.java index 422e98d..4d0f63e 100644 --- a/src/main/java/baubles/client/gui/GuiEvents.java +++ b/src/main/java/baubles/client/gui/GuiEvents.java @@ -3,8 +3,8 @@ package baubles.client.gui; import java.lang.reflect.Method; import baubles.common.network.PacketHandler; -import baubles.common.network.PacketOpenBaublesInventory; -import baubles.common.network.PacketOpenNormalInventory; +import baubles.common.network.server.PacketOpenBaublesInventory; +import baubles.common.network.server.PacketOpenNormalInventory; import cpw.mods.fml.common.eventhandler.SubscribeEvent; import cpw.mods.fml.relauncher.Side; import cpw.mods.fml.relauncher.SideOnly; diff --git a/src/main/java/baubles/client/gui/GuiPlayerExpanded.java b/src/main/java/baubles/client/gui/GuiPlayerExpanded.java index f09235c..8e94f1e 100644 --- a/src/main/java/baubles/client/gui/GuiPlayerExpanded.java +++ b/src/main/java/baubles/client/gui/GuiPlayerExpanded.java @@ -9,11 +9,15 @@ import cpw.mods.fml.common.Optional; import net.minecraft.client.Minecraft; import net.minecraft.client.entity.EntityPlayerSP; import net.minecraft.client.gui.inventory.GuiContainer; +import net.minecraft.client.gui.inventory.GuiContainerCreative; import net.minecraft.client.gui.inventory.GuiInventory; import net.minecraft.client.resources.I18n; +import net.minecraft.inventory.Slot; import net.minecraft.item.ItemStack; import net.minecraft.potion.Potion; import net.minecraft.potion.PotionEffect; +import net.minecraft.util.MathHelper; + import org.lwjgl.input.Mouse; import org.lwjgl.opengl.GL11; @@ -52,6 +56,15 @@ public class GuiPlayerExpanded extends GuiContainer implements INEIGuiHandler { public boolean showActivePotionEffects; + private boolean widthTooNarrow; + + /** Amount scrolled in Creative mode inventory (0 = top, 1 = bottom) */ + private float currentScroll; + /** True if the scrollbar is being dragged */ + private boolean isScrolling; + /** True if the left mouse button was held down last time drawScreen was called. */ + private boolean wasClicking; + public GuiPlayerExpanded(EntityPlayer player) { super(new ContainerPlayerExpanded(player.inventory, !player.worldObj.isRemote, player)); allowUserInput = true; @@ -63,7 +76,7 @@ public class GuiPlayerExpanded extends GuiContainer implements INEIGuiHandler { @Override public void updateScreen() { try { - ((ContainerPlayerExpanded)inventorySlots).baubles.blockEvents = false; + ((ContainerPlayerExpanded) inventorySlots).baubles.blockEvents = false; } catch (Exception e) {} } @@ -75,6 +88,8 @@ public class GuiPlayerExpanded extends GuiContainer implements INEIGuiHandler { buttonList.clear(); super.initGui(); + this.widthTooNarrow = this.width < 379; + if (!this.mc.thePlayer.getActivePotionEffects().isEmpty()) { this.showActivePotionEffects = true; } @@ -86,9 +101,10 @@ public class GuiPlayerExpanded extends GuiContainer implements INEIGuiHandler { @Override public void drawScreen(int mouseX, int mouseY, float partialTicks) { super.drawScreen(mouseX, mouseY, partialTicks); - boolean flag = Mouse.isButtonDown(0); xSizeFloat = (float) mouseX; ySizeFloat = (float) mouseY; + + handleScrollbar(mouseX, mouseY); } @Override @@ -130,9 +146,17 @@ public class GuiPlayerExpanded extends GuiContainer implements INEIGuiHandler { //bauble slot backgrounds for (int slotIndex = 0; slotIndex < BaubleExpandedSlots.slotLimit; slotIndex++) { String slotType = BaubleExpandedSlots.getSlotType(slotIndex); + int slotPage = 1; if (BaublesConfig.showUnusedSlots || !slotType.equals(BaubleExpandedSlots.unknownType)) { //Slot slot = (Slot)inventorySlots.inventorySlots.get(slotIndex + 4); - drawTexturedModalRect(slotStartX + (slotOffset * (slotIndex / 4)), slotStartY + (slotOffset * slotIndex), 200, 0, 18, 18); + + + if (BaubleExpandedSlots.slotsCurrentlyUsed() <= 8 && slotPage == 1) { + drawTexturedModalRect(slotStartX + (slotOffset * (slotIndex / 4)), slotStartY + (slotOffset * slotIndex), 200, 0, 18, 18); + } + if (BaubleExpandedSlots.slotsCurrentlyUsed() >= 9 && BaubleExpandedSlots.slotsCurrentlyUsed() >= 16 && slotPage == 2) { + drawTexturedModalRect(slotStartX + (slotOffset * (slotIndex / 4)), slotStartY + (slotOffset * slotIndex), 200, 0, 18, 18); + } } } } @@ -181,6 +205,56 @@ public class GuiPlayerExpanded extends GuiContainer implements INEIGuiHandler { } } + private boolean needsScrollBars() { + return ((ContainerPlayerExpanded) this.inventorySlots).canScroll(); + } + + private void handleScrollbar(int mouseX, int mouseY) { + boolean flag = Mouse.isButtonDown(0); + int k = this.guiLeft; + int l = this.guiTop; + int i1 = k - 34; + int j1 = l + 12; + int k1 = i1 + 14; + int l1 = j1 + 139; + + if (!this.wasClicking && flag && mouseX >= i1 && mouseY >= j1 && mouseX < k1 && mouseY < l1) { + this.isScrolling = this.needsScrollBars(); + } + + if (!flag) { + this.isScrolling = false; + } + + this.wasClicking = flag; + + if (this.isScrolling) { + this.currentScroll = ((float)(mouseY - j1) - 7.5F) / ((float)(l1 - j1) - 15.0F); + + if (this.currentScroll < 0.0F) { + this.currentScroll = 0.0F; + } + + if (this.currentScroll > 1.0F) { + this.currentScroll = 1.0F; + } + + ((ContainerPlayerExpanded) this.inventorySlots).scrollTo(this.currentScroll); + } + } + + @Override + public void handleMouseInput() { + super.handleMouseInput(); + int wheel = Mouse.getEventDWheel(); + if (this.needsScrollBars()) { + int i = ((ContainerPlayerExpanded)this.inventorySlots).baubleSlotMap.size(); + this.currentScroll = (float)((double)this.currentScroll - wheel / (double) i); + this.currentScroll = MathHelper.clamp_float(this.currentScroll, 0.0F, 1.0F); + ((ContainerPlayerExpanded)this.inventorySlots).scrollTo(this.currentScroll); + } + } + @Override protected void actionPerformed(GuiButton button) { if (button.id == 0) { diff --git a/src/main/java/baubles/common/container/ContainerPlayerExpanded.java b/src/main/java/baubles/common/container/ContainerPlayerExpanded.java index eb0b108..a604161 100644 --- a/src/main/java/baubles/common/container/ContainerPlayerExpanded.java +++ b/src/main/java/baubles/common/container/ContainerPlayerExpanded.java @@ -5,9 +5,14 @@ import baubles.api.expanded.BaubleExpandedSlots; import baubles.api.expanded.IBaubleExpanded; import baubles.common.BaublesConfig; import baubles.common.lib.PlayerHandler; +import baubles.common.network.PacketHandler; +import baubles.common.network.client.PacketScrollBaubleClient; +import baubles.common.network.server.PacketScrollBaubleServer; +import codechicken.nei.api.IInfiniteItemHandler; import cpw.mods.fml.relauncher.Side; import cpw.mods.fml.relauncher.SideOnly; import net.minecraft.entity.player.EntityPlayer; +import net.minecraft.entity.player.EntityPlayerMP; import net.minecraft.entity.player.InventoryPlayer; import net.minecraft.inventory.Container; import net.minecraft.inventory.IInventory; @@ -21,17 +26,27 @@ import net.minecraft.item.ItemStack; import net.minecraft.item.crafting.CraftingManager; import net.minecraft.util.IIcon; +import javax.annotation.Nonnull; +import java.util.Arrays; +import java.util.LinkedHashMap; +import java.util.SortedMap; +import java.util.TreeMap; + public class ContainerPlayerExpanded extends Container { + public SortedMap baubleSlotMap = new TreeMap<>(); + SortedMap shownFilteredSlots = new TreeMap<>(); public InventoryCrafting craftMatrix = new InventoryCrafting(this, 2, 2); public IInventory craftResult = new InventoryCraftResult(); public InventoryBaubles baubles; + private int lastScrollIndex; + public boolean isLocalWorld; private final EntityPlayer thePlayer; public ContainerPlayerExpanded(InventoryPlayer playerInv, boolean par2, EntityPlayer player) { - this.isLocalWorld = par2; + this.isLocalWorld = player.worldObj.isRemote; this.thePlayer = player; baubles = new InventoryBaubles(player); baubles.setEventHandler(this); @@ -72,15 +87,6 @@ public class ContainerPlayerExpanded extends Container { } final int slotOffset = 18; - - //bauble slots - for(i = 0; i < BaubleExpandedSlots.slotLimit; i++) { - String slotType = BaubleExpandedSlots.getSlotType(i); - if(BaublesConfig.showUnusedSlots || !slotType.equals(BaubleExpandedSlots.unknownType)) { - addSlotToContainer(new SlotBauble(baubles, slotType, i, -18, 12 + (slotOffset * i))); - } - } - final int slotStartY = 8; //inventory slots @@ -96,7 +102,7 @@ public class ContainerPlayerExpanded extends Container { } this.onCraftMatrixChanged(this.craftMatrix); - + this.scrollToIndex(0); } @Override @@ -121,6 +127,101 @@ public class ContainerPlayerExpanded extends Container { } } + public void scrollToIndex(int indexIn) { + String[] numberOfActiveSlots = BaubleExpandedSlots.getCurrentSlotAssignments(); + final int visibleBaubleSlots = BaublesConfig.showUnusedSlots ? BaubleExpandedSlots.slotLimit : BaubleExpandedSlots.slotsCurrentlyUsed(); + //System.out.print(Arrays.toString(numberOfActiveSlots)); + + int slots = 0; + int yOffset = 12; + int index = 0; + this.inventorySlots.subList(45, this.inventorySlots.size()).clear(); + this.inventoryItemStacks.subList(45, this.inventoryItemStacks.size()).clear(); + + for (int i = 0; i < numberOfActiveSlots.length && i < BaubleExpandedSlots.slotLimit; i++) { + if (!numberOfActiveSlots[i].equals(BaubleExpandedSlots.unknownType)) { + baubleSlotMap.put(i, numberOfActiveSlots[i]); + //System.out.println(baubleSlotMap); + } + } + //System.out.println(baubleSlotMap); + for (int identifier : baubleSlotMap.keySet()) { + //System.out.println(identifier + " " + baubleSlotMap.get(identifier)); + + String shownSlot = baubleSlotMap.get(identifier); + + //System.out.println(identifier + " " + baubleSlotMap.get(identifier)); + for (int i = 0; i + 1 < shownSlot.length() && slots < 8; i++) { + String slotType = BaubleExpandedSlots.getSlotType(i); + //System.out.println(slotType + i); + if (index >= indexIn) { + addSlotToContainer(new SlotBauble(baubles, slotType, i, -18, yOffset)); + yOffset += 18; + slots++; + //System.out.println(shownSlot + " " + identifier); + } + index++; + } + } + if (!this.isLocalWorld) { + PacketHandler.INSTANCE.sendTo(new PacketScrollBaubleServer(this.windowId, indexIn), (EntityPlayerMP) this.thePlayer); + } + lastScrollIndex = indexIn; + } + + public void scrollTo(float offset) { + + if (!canScroll()) return; + + if (offset < 0) offset = 0; + if (offset > 1) offset = 1; + + + int k = Math.min (baubleSlotMap.size(), 8); + + int j = (int)((double)(offset * (float) k) + 0.5D); + + if (j < 0) { + j = 0; + } + + if (j == this.lastScrollIndex) { + return; + } + + shownFilteredSlots = baubleSlotMap.subMap(this.lastScrollIndex * (int) offset, (int)(offset * (float) k)); + System.out.println(lastScrollIndex + " " + offset + " " + k); + System.out.println(shownFilteredSlots); + + if (this.isLocalWorld) { + PacketHandler.INSTANCE.sendToServer(new PacketScrollBaubleClient(this.windowId, j)); + } + //baubleSlotMap.subMap( , (int)(offset * (float) k)); + /* + for () + this.baubleSlotMap.subMap(); + int k = (baubleSlotMap.get()); + int j = (int)((double)(pos * (float) k) + 0.5D); + + if (j < 0) { + j = 0; + } + + if (j == this.lastScrollIndex) { + return; + } + */ + + + } + + public boolean canScroll() { + if (BaubleExpandedSlots.slotsCurrentlyUsed() > 8) { + return true; + } + return false; + } + @Override public boolean canInteractWith(EntityPlayer player) { return true; @@ -129,6 +230,7 @@ public class ContainerPlayerExpanded extends Container { /** * Called when a player shift-clicks on a slot. You must override this or you will crash when someone does that. */ + @Nonnull @Override public ItemStack transferStackInSlot(EntityPlayer player, int slotIndex) { ItemStack returnStack = null; @@ -209,9 +311,9 @@ public class ContainerPlayerExpanded extends Container { } private void unequipBauble(ItemStack stack) { -// if (stack.getItem() instanceof IBauble) { -// ((IBauble)stack.getItem()).onUnequipped(stack, thePlayer); -// } + //if (stack.getItem() instanceof IBauble) { + // ((IBauble)stack.getItem()).onUnequipped(stack, thePlayer); + //} } @Override diff --git a/src/main/java/baubles/common/container/InventoryBaubles.java b/src/main/java/baubles/common/container/InventoryBaubles.java index 34a12fe..8b196fd 100644 --- a/src/main/java/baubles/common/container/InventoryBaubles.java +++ b/src/main/java/baubles/common/container/InventoryBaubles.java @@ -10,7 +10,7 @@ import baubles.api.expanded.IBaubleExpanded; import baubles.common.Baubles; import baubles.common.lib.ItemStackHelper; import baubles.common.network.PacketHandler; -import baubles.common.network.PacketSyncBauble; +import baubles.common.network.client.PacketSyncBauble; import net.minecraft.entity.Entity; import net.minecraft.entity.item.EntityItem; import net.minecraft.entity.player.EntityPlayer; diff --git a/src/main/java/baubles/common/event/KeyHandler.java b/src/main/java/baubles/common/event/KeyHandler.java index 3a02827..8afe12d 100644 --- a/src/main/java/baubles/common/event/KeyHandler.java +++ b/src/main/java/baubles/common/event/KeyHandler.java @@ -3,7 +3,7 @@ package baubles.common.event; import org.lwjgl.input.Keyboard; import baubles.common.network.PacketHandler; -import baubles.common.network.PacketOpenBaublesInventory; +import baubles.common.network.server.PacketOpenBaublesInventory; import cpw.mods.fml.client.FMLClientHandler; import cpw.mods.fml.client.registry.ClientRegistry; import cpw.mods.fml.common.eventhandler.SubscribeEvent; @@ -15,10 +15,10 @@ import net.minecraft.client.settings.KeyBinding; import net.minecraft.util.StatCollector; public class KeyHandler { - - public KeyBinding key = new KeyBinding(StatCollector.translateToLocal("keybind.baublesinventory"), + + public KeyBinding key = new KeyBinding(StatCollector.translateToLocal("keybind.baublesinventory"), Keyboard.KEY_B, "key.categories.inventory"); - + public KeyHandler() { ClientRegistry.registerKeyBinding(key); } diff --git a/src/main/java/baubles/common/network/PacketHandler.java b/src/main/java/baubles/common/network/PacketHandler.java index 00eac45..11cca03 100644 --- a/src/main/java/baubles/common/network/PacketHandler.java +++ b/src/main/java/baubles/common/network/PacketHandler.java @@ -1,6 +1,11 @@ package baubles.common.network; import baubles.common.Baubles; +import baubles.common.network.client.PacketScrollBaubleClient; +import baubles.common.network.client.PacketSyncBauble; +import baubles.common.network.server.PacketOpenBaublesInventory; +import baubles.common.network.server.PacketOpenNormalInventory; +import baubles.common.network.server.PacketScrollBaubleServer; import cpw.mods.fml.common.network.NetworkRegistry; import cpw.mods.fml.common.network.simpleimpl.SimpleNetworkWrapper; import cpw.mods.fml.relauncher.Side; @@ -14,7 +19,9 @@ public class PacketHandler INSTANCE.registerMessage(PacketOpenBaublesInventory.class, PacketOpenBaublesInventory.class, 0, Side.SERVER); INSTANCE.registerMessage(PacketOpenNormalInventory.class, PacketOpenNormalInventory.class, 1, Side.SERVER); INSTANCE.registerMessage(PacketSyncBauble.class, PacketSyncBauble.class, 2, Side.CLIENT); + INSTANCE.registerMessage(PacketScrollBaubleClient.class, PacketScrollBaubleClient.class, 3, Side.CLIENT); + INSTANCE.registerMessage(PacketScrollBaubleServer.class, PacketScrollBaubleServer.class, 4, Side.SERVER); } - - + + } diff --git a/src/main/java/baubles/common/network/client/PacketScrollBaubleClient.java b/src/main/java/baubles/common/network/client/PacketScrollBaubleClient.java new file mode 100644 index 0000000..101eaf1 --- /dev/null +++ b/src/main/java/baubles/common/network/client/PacketScrollBaubleClient.java @@ -0,0 +1,46 @@ +package baubles.common.network.client; + +import baubles.common.container.ContainerPlayerExpanded; +import baubles.common.network.server.PacketOpenNormalInventory; +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.inventory.Container; + +public class PacketScrollBaubleClient implements IMessage, IMessageHandler { + + int windowId; + int index; + + public PacketScrollBaubleClient() {}; + + public PacketScrollBaubleClient(int windowId, int index) { + this.windowId = windowId; + this.index = index; + } + + @Override + public void fromBytes(ByteBuf buf) { + windowId = buf.readInt(); + index = buf.readInt(); + } + + @Override + public void toBytes(ByteBuf buf) { + buf.writeInt(windowId); + buf.writeInt(index); + } + + + @Override + public IMessage onMessage(PacketScrollBaubleClient message, MessageContext ctx) { + EntityPlayerMP serverPlayer = ctx.getServerHandler().playerEntity; + Container container = serverPlayer.openContainer; + if (container instanceof ContainerPlayerExpanded && container.windowId == message.windowId) { + ((ContainerPlayerExpanded) container).scrollToIndex(message.index); + } + return null; + } +} diff --git a/src/main/java/baubles/common/network/PacketSyncBauble.java b/src/main/java/baubles/common/network/client/PacketSyncBauble.java similarity index 95% rename from src/main/java/baubles/common/network/PacketSyncBauble.java rename to src/main/java/baubles/common/network/client/PacketSyncBauble.java index 3f6f3ca..800694c 100644 --- a/src/main/java/baubles/common/network/PacketSyncBauble.java +++ b/src/main/java/baubles/common/network/client/PacketSyncBauble.java @@ -1,4 +1,4 @@ -package baubles.common.network; +package baubles.common.network.client; import java.io.IOException; @@ -15,13 +15,13 @@ import net.minecraft.network.PacketBuffer; import net.minecraft.world.World; public class PacketSyncBauble implements IMessage, IMessageHandler { - + int slot; int playerId; ItemStack bauble=null; - + public PacketSyncBauble() {} - + public PacketSyncBauble(EntityPlayer player, int slot) { this.slot = slot; this.bauble = PlayerHandler.getPlayerBaubles(player).getStackInSlot(slot); @@ -37,7 +37,7 @@ public class PacketSyncBauble implements IMessage, IMessageHandler { - + public PacketOpenBaublesInventory() {} - + public PacketOpenBaublesInventory(EntityPlayer player) {} @Override diff --git a/src/main/java/baubles/common/network/PacketOpenNormalInventory.java b/src/main/java/baubles/common/network/server/PacketOpenNormalInventory.java similarity index 91% rename from src/main/java/baubles/common/network/PacketOpenNormalInventory.java rename to src/main/java/baubles/common/network/server/PacketOpenNormalInventory.java index 214d23d..dfda11c 100644 --- a/src/main/java/baubles/common/network/PacketOpenNormalInventory.java +++ b/src/main/java/baubles/common/network/server/PacketOpenNormalInventory.java @@ -1,4 +1,4 @@ -package baubles.common.network; +package baubles.common.network.server; import cpw.mods.fml.common.network.simpleimpl.IMessage; import cpw.mods.fml.common.network.simpleimpl.IMessageHandler; @@ -7,9 +7,9 @@ import io.netty.buffer.ByteBuf; import net.minecraft.entity.player.EntityPlayer; public class PacketOpenNormalInventory implements IMessage, IMessageHandler { - + public PacketOpenNormalInventory() {} - + public PacketOpenNormalInventory(EntityPlayer player) {} @Override @@ -20,7 +20,7 @@ public class PacketOpenNormalInventory implements IMessage, IMessageHandler { + + int windowId; + int index; + + public PacketScrollBaubleServer(){} + public PacketScrollBaubleServer(int windowId, int index) { + this.windowId = windowId; + this.index = index; + } + + @Override + public void fromBytes(ByteBuf buf) { + windowId = buf.readInt(); + index = buf.readInt(); + } + + @Override + public void toBytes(ByteBuf buf) { + buf.writeInt(windowId); + buf.writeInt(index); + } + + + @Override + public IMessage onMessage(PacketScrollBaubleServer message, MessageContext ctx) { + EntityPlayerSP sp = Minecraft.getMinecraft().thePlayer; + Container container = sp.openContainer; + if (container instanceof ContainerPlayerExpanded && container.windowId == message.windowId) { + ((ContainerPlayerExpanded) container).scrollToIndex(message.index); + } + return null; + } +}