Still a work in progress, but I finally got the scrollbar to work with both the wheel and the mouse

This commit is contained in:
Ethryan committed 2025-06-09 00:13:58 +02:00
1 parent a2e3568c1d
commit ce3f82a756
11 files changed
+312 -38

No files matched your search

@@ -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;
@@ -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) {
@@ -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<Integer, String> baubleSlotMap = new TreeMap<>();
SortedMap<Integer, String> 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
@@ -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;
@@ -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);
}
@@ -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);
}
}
@@ -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<PacketScrollBaubleClient, IMessage> {
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;
}
}
@@ -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<PacketSyncBauble, IMessage> {
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<PacketSyncBau
}
@Override
public void fromBytes(ByteBuf buffer)
public void fromBytes(ByteBuf buffer)
{
slot = buffer.readByte();
playerId = buffer.readInt();
@@ -1,4 +1,4 @@
package baubles.common.network;
package baubles.common.network.server;
import baubles.common.Baubles;
import cpw.mods.fml.common.network.simpleimpl.IMessage;
@@ -8,9 +8,9 @@ import io.netty.buffer.ByteBuf;
import net.minecraft.entity.player.EntityPlayer;
public class PacketOpenBaublesInventory implements IMessage, IMessageHandler<PacketOpenBaublesInventory, IMessage> {
public PacketOpenBaublesInventory() {}
public PacketOpenBaublesInventory(EntityPlayer player) {}
@Override
@@ -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<PacketOpenNormalInventory, IMessage> {
public PacketOpenNormalInventory() {}
public PacketOpenNormalInventory(EntityPlayer player) {}
@Override
@@ -20,7 +20,7 @@ public class PacketOpenNormalInventory implements IMessage, IMessageHandler<Pack
@Override
public IMessage onMessage(PacketOpenNormalInventory message, MessageContext ctx) {
ctx.getServerHandler().playerEntity.openContainer.onContainerClosed(ctx.getServerHandler().playerEntity);
ctx.getServerHandler().playerEntity.openContainer.onContainerClosed(ctx.getServerHandler().playerEntity);
ctx.getServerHandler().playerEntity.openContainer = ctx.getServerHandler().playerEntity.inventoryContainer;
return null;
}
@@ -0,0 +1,45 @@
package baubles.common.network.server;
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.client.Minecraft;
import net.minecraft.client.entity.EntityPlayerSP;
import net.minecraft.inventory.Container;
public class PacketScrollBaubleServer implements IMessage, IMessageHandler<PacketScrollBaubleServer, IMessage> {
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;
}
}