From 8248101e0e396b5eb5ae8916e792aee97b38f887 Mon Sep 17 00:00:00 2001 From: broscolotos Date: Fri, 8 Sep 2023 05:32:27 -0500 Subject: [PATCH] Simple seat GUI stage 1 A simple seat picking GUI, and the networking for it, that allows you to choose which seat you are sitting in. Everything with more than 1 seat gets the button to open this GUI. More improvements to this are coming in the following commits --- src/main/java/ebf/tim/TrainsInMotion.java | 2 + src/main/java/ebf/tim/gui/GUISeatManager.java | 179 ++++++++++++++++++ src/main/java/ebf/tim/gui/GUITransport.java | 34 +++- .../ebf/tim/networking/PacketSeatUpdate.java | 74 ++++++++ .../java/ebf/tim/utility/ClientProxy.java | 4 + .../java/ebf/tim/utility/CommonProxy.java | 4 + 6 files changed, 290 insertions(+), 7 deletions(-) create mode 100644 src/main/java/ebf/tim/gui/GUISeatManager.java create mode 100644 src/main/java/ebf/tim/networking/PacketSeatUpdate.java diff --git a/src/main/java/ebf/tim/TrainsInMotion.java b/src/main/java/ebf/tim/TrainsInMotion.java index 2ca0d87347..6dfbd8bedf 100644 --- a/src/main/java/ebf/tim/TrainsInMotion.java +++ b/src/main/java/ebf/tim/TrainsInMotion.java @@ -184,6 +184,8 @@ public void init(FMLInitializationEvent event) { TrainsInMotion.keyChannel.registerMessage(HANDLERS[5], PacketCraftingPage.class, 6, Side.SERVER); TrainsInMotion.updateChannel = NetworkRegistry.INSTANCE.newSimpleChannel("TiM.update"); TrainsInMotion.updateChannel.registerMessage(HANDLERS[6], PacketUpdateClients.class, 7, Side.CLIENT); + TrainsInMotion.updateChannel.registerMessage(PacketSeatUpdate.Handler.class, PacketSeatUpdate.class, 8, Side.CLIENT); + TrainsInMotion.updateChannel.registerMessage(PacketSeatUpdate.Handler.class, PacketSeatUpdate.class, 9, Side.SERVER); TrainsInMotion.trackChannel = NetworkRegistry.INSTANCE.newSimpleChannel("TiM.track"); diff --git a/src/main/java/ebf/tim/gui/GUISeatManager.java b/src/main/java/ebf/tim/gui/GUISeatManager.java new file mode 100644 index 0000000000..e7c1ce5ddb --- /dev/null +++ b/src/main/java/ebf/tim/gui/GUISeatManager.java @@ -0,0 +1,179 @@ +package ebf.tim.gui; + +import ebf.tim.TrainsInMotion; +import ebf.tim.api.SkinRegistry; +import ebf.tim.api.TransportSkin; +import ebf.tim.entities.EntitySeat; +import ebf.tim.entities.GenericRailTransport; +import ebf.tim.networking.PacketPaint; +import ebf.tim.networking.PacketSeatUpdate; +import ebf.tim.utility.ClientProxy; +import ebf.tim.utility.ClientUtil; +import ebf.tim.utility.CommonUtil; +import ebf.tim.utility.EventManager; +import net.minecraft.client.Minecraft; +import net.minecraft.client.entity.AbstractClientPlayer; +import net.minecraft.client.gui.FontRenderer; +import net.minecraft.client.gui.GuiScreen; +import net.minecraft.client.gui.ScaledResolution; +import net.minecraft.client.renderer.RenderHelper; +import net.minecraft.entity.EntityLivingBase; +import net.minecraft.entity.player.EntityPlayer; +import net.minecraft.util.ResourceLocation; +import org.lwjgl.input.Keyboard; +import org.lwjgl.opengl.GL11; +import org.lwjgl.util.glu.Project; + +import java.util.*; + + +/** + *

Transport GUI

+ * used to draw the GUI for choosing a seat while in a piece of rollingstock (get here from a button in the normal UI). + * @author broscolotos + */ +public class GUISeatManager extends GUIPaintBucket { + + public ArrayList filledSeatIDs = new ArrayList<>(); + public EntitySeat currentSeat; + public GUISeatManager(GenericRailTransport transport) { + super(transport); + } + + @Override + public void initGui() { + if(entity !=null && entity.getRiderOffsets().length > 0) { + + } + } + @Override + public void updateScreen() {} + + public static int percentTop(int value){return (int)(guiTop*(value*0.01f));} + public static int percentLeft(int value){return (int)(guiLeft*(value*0.01f));} + + @Override + public void drawScreen(int parWidth, int parHeight, float p_73863_3_) { + this.drawDefaultBackground(); + super.drawScreen(parWidth, parHeight, p_73863_3_); + guiLeft=new ScaledResolution(Minecraft.getMinecraft(), Minecraft.getMinecraft().displayWidth, Minecraft.getMinecraft().displayHeight).getScaledWidth(); + guiTop=new ScaledResolution(Minecraft.getMinecraft(), Minecraft.getMinecraft().displayWidth, Minecraft.getMinecraft().displayHeight).getScaledHeight(); + + if (entity.getRiderOffsets().length==0 || entity.getRiderOffsets() == null){ + return; + } + for(EntitySeat seat : entity.seats) { + if (seat.getPassenger() != null) { + if (seat.getPassenger() == Minecraft.getMinecraft().thePlayer) { + currentSeat = seat; + } + filledSeatIDs.add(seat.getEntityId()); + } + } + if(filledSeatIDs.size() == entity.seats.size()) { //remove after GUI is sorted out + return; + } + switch(guiScreen) { + case 0:{guiSeatManager();defineButtons();break;} + } + } + @Override + public boolean doesGuiPauseGame() {return false;} + @Override + public void defineButtons(){ + switch (guiScreen){ + case 0:{ + buttonList =new ArrayList(); + //this is for debugging. After its all sorted we will have a GUI with all of the seats laid out on a grid + int column = 0; + int activeRow = 0; + for (int i=0;ipercentTop(60)) { + column++; + activeRow=0; + } + + if (entity.seats.get(i).getPassenger() instanceof EntityPlayer) { + //mc.getTextureManager().bindTexture(((AbstractClientPlayer) entity.riddenByEntity).getLocationSkin()); + buttonList.add( + new GUIButton(percentLeft(50)-240+(column*120), percentTop(25)+(28*activeRow+1), fontRendererObj.getStringWidth(entity.seats.get(i).getPassenger().getCommandSenderName())+7,20,entity.seats.get(i).getPassenger().getCommandSenderName()) { + @Override + public String getHoverText() { + return "Seat is currently occupied"; + } + @Override + public void onClick() {} + @Override + public FontRenderer getFont(){return fontRendererObj;} + } + ); + } else { //TODO: add an else if for things that aren't players. this becomes important for stuff like stock cars or putting villagers in player seats + buttonList.add( + new GUIButton( percentLeft(50)-240+(column*120), percentTop(25)+(28*activeRow+1), fontRendererObj.getStringWidth("Empty Seat")+6,20,"Empty Seat") { + @Override + public String getHoverText() { + return "Seat is currently Empty"; + } + @Override + public void onClick() { + /*int nextIndex = 0; + if (entity.seats.indexOf(currentSeat) == 0) { + nextIndex = entity.seats.size()-1; + } else { + nextIndex = entity.seats.indexOf(currentSeat)-1; + }*/ + ; + EntityLivingBase passenger = currentSeat.getPassenger(); +/* currentSeat.removePassenger(passenger); + entity.seats.get(buttonList.indexOf(this)).addPassenger(passenger); + currentSeat = entity.seats.get(buttonList.indexOf(this));*/ + TrainsInMotion.updateChannel.sendToServer(new PacketSeatUpdate(entity.getEntityId(),passenger.getEntityId(),entity.seats.indexOf(currentSeat),buttonList.indexOf(this))); + //currentSeat = entity.seats.get(buttonList.indexOf(this)); + //TODO: need a way to tell the server that the player and previous EntitySeat need updated. + + } + @Override + public FontRenderer getFont(){return fontRendererObj;} + } + ); + } + activeRow++; + } + break; + } + } + } + + + + + public void guiSeatManager(){ + mc.getTextureManager().bindTexture(ClientUtil.vanillaInventory); + GL11.glDisable(GL11.GL_LIGHTING); + GL11.glPushMatrix(); + int rows=0; + //draw the character backgrounds. + //make a loop that will make a new row every 5 items + for (int i=0;i=5*(rows+1)){ + rows++; + } + ClientUtil.drawTexturedRect(guiLeft + 106 + (30*(i-(rows * 5))), guiTop + 30+(30*rows), 54, 51, 27, 27, 20, 20); + } + //make a new loop that does the same as above but binds the character's face rather than the inventory slot background. + for (int i=0;i=5*(rows+1)){ + rows++; + } + if (i==0 && entity.riddenByEntity instanceof AbstractClientPlayer){ + mc.getTextureManager().bindTexture(((AbstractClientPlayer) entity.riddenByEntity).getLocationSkin()); + ClientUtil.drawTexturedRect(guiLeft + 108 + (30*(i-(rows * 5))), guiTop + 32+(30*rows), 30, 70, 23, 23, 36, 56); + } else if (i>0 && entity.seats.get(i-1).riddenByEntity instanceof AbstractClientPlayer){ + mc.getTextureManager().bindTexture(((AbstractClientPlayer) entity.seats.get(i-1).riddenByEntity).getLocationSkin()); + ClientUtil.drawTexturedRect(guiLeft + 108 + (30*(i-(rows * 5))), guiTop + 32+(30*rows), 30, 70, 23, 23, 36, 56); + } + } + GL11.glPopMatrix(); + GL11.glEnable(GL11.GL_LIGHTING); + } +} diff --git a/src/main/java/ebf/tim/gui/GUITransport.java b/src/main/java/ebf/tim/gui/GUITransport.java index be09e54fa0..eba0a4ada3 100644 --- a/src/main/java/ebf/tim/gui/GUITransport.java +++ b/src/main/java/ebf/tim/gui/GUITransport.java @@ -211,7 +211,6 @@ public void onClick() { @Override public FontRenderer getFont(){return fontRendererObj;} }); - this.buttons.add(new GUIButton((int)guiLeft + 238, (int)guiTop + 166, 18, 18){ @Override @@ -264,6 +263,27 @@ public void onClick() { @Override public FontRenderer getFont(){return fontRendererObj;} }); + if (transport.getRiderOffsets().length > 0 || transport.getRiderOffsets() != null) { + this.buttons.add(new GUIButton((int)guiLeft+166,(int)guiTop+166, 18,18) { + @Override + public String getHoverText() { + return "gui.seats"; + } + + @Override + public int[] getColor(){ + return null; + } + + @Override + public void onClick() { + TrainsInMotion.proxy.seatGUI(player,transport); + } + + @Override + public FontRenderer getFont(){return fontRendererObj;} + }); + } //train specific if (transport instanceof EntityTrainCore) { if (player.capabilities.isCreativeMode) { @@ -391,16 +411,16 @@ private void renderTrainInventory(Minecraft mc){ * todo: set this up just to render the passenger icons and empty seats above the player inventory */ private void renderPassengerInventory(Minecraft mc){ - + mc.getTextureManager().bindTexture(ClientUtil.vanillaInventory); + GL11.glDisable(GL11.GL_LIGHTING); int rows=0; //draw the character backgrounds. - mc.getTextureManager().bindTexture(ClientUtil.vanillaInventory); //make a loop that will make a new row every 5 items for (int i=0;i=5*(rows+1)){ rows++; } - ClientUtil.drawTexturedRect(guiLeft + 7 + (30*(i-(rows * 5))), guiTop + 30+(30*rows), 54, 51, 27, 27, 20, 20); + ClientUtil.drawTexturedRect(guiLeft + 106 + (30*(i-(rows * 5))), guiTop + 30+(30*rows), 54, 51, 27, 27, 20, 20); } //make a new loop that does the same as above but binds the character's face rather than the inventory slot background. for (int i=0;i0 && transport.seats.get(i-1).riddenByEntity instanceof AbstractClientPlayer){ mc.getTextureManager().bindTexture(((AbstractClientPlayer) transport.seats.get(i-1).riddenByEntity).getLocationSkin()); - ClientUtil.drawTexturedRect(guiLeft + 10 + (30*(i-(rows * 5))), guiTop + 32+(30*rows), 30, 70, 22, 22, 36, 56); + ClientUtil.drawTexturedRect(guiLeft + 108 + (30*(i-(rows * 5))), guiTop + 32+(30*rows), 30, 70, 23, 23, 36, 56); } } - + GL11.glEnable(GL11.GL_LIGHTING); } /** diff --git a/src/main/java/ebf/tim/networking/PacketSeatUpdate.java b/src/main/java/ebf/tim/networking/PacketSeatUpdate.java new file mode 100644 index 0000000000..f37bbaa876 --- /dev/null +++ b/src/main/java/ebf/tim/networking/PacketSeatUpdate.java @@ -0,0 +1,74 @@ +package ebf.tim.networking; + +import cpw.mods.fml.common.network.NetworkRegistry; +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 cpw.mods.fml.relauncher.Side; +import ebf.tim.TrainsInMotion; +import ebf.tim.entities.EntitySeat; +import ebf.tim.entities.GenericRailTransport; +import io.netty.buffer.ByteBuf; +import net.minecraft.client.Minecraft; +import net.minecraft.entity.Entity; +/** + *

Seat Packet

+ * bi-directional packet for managing seat changes while in a piece of stock. + * @author broscolotos + * @author 02Skaplan + */ +public class PacketSeatUpdate implements IMessage { + /**the ID of the entity to dismount from*/ + private int rollingStockId, playerId, oldSeatIndex, newSeatIndex; + + public PacketSeatUpdate() {} + public PacketSeatUpdate(int rollingStockId, int playerId, int oldSeatIndex, int newSeatIndex) { + this.rollingStockId = rollingStockId; + this.playerId = playerId; + this.oldSeatIndex = oldSeatIndex; + this.newSeatIndex = newSeatIndex; + } + /**reads the packet on server to get the variables from the Byte Buffer*/ + @Override + public void fromBytes(ByteBuf bbuf) { + rollingStockId= bbuf.readInt(); + playerId=bbuf.readInt(); + oldSeatIndex=bbuf.readInt(); + newSeatIndex=bbuf.readInt(); + } + /**puts the variables into a Byte Buffer so they can be sent to server*/ + @Override + public void toBytes(ByteBuf bbuf) { + bbuf.writeInt(rollingStockId); + bbuf.writeInt(playerId); + bbuf.writeInt(oldSeatIndex); + bbuf.writeInt(newSeatIndex); + } + + public static class Handler implements IMessageHandler { + @Override public IMessage onMessage(PacketSeatUpdate message, MessageContext ctx) { + GenericRailTransport rollingStockEntity; + Entity playerEntity; + EntitySeat oldSeat; + EntitySeat newSeat; + if (ctx.side == Side.SERVER) { + rollingStockEntity = (GenericRailTransport) ctx.getServerHandler().playerEntity.worldObj.getEntityByID(message.rollingStockId); + playerEntity = ctx.getServerHandler().playerEntity.worldObj.getEntityByID(message.playerId); + + } else { + rollingStockEntity = (GenericRailTransport) Minecraft.getMinecraft().theWorld.getEntityByID(message.rollingStockId); + playerEntity = Minecraft.getMinecraft().theWorld.getEntityByID(message.playerId); + } + oldSeat = rollingStockEntity.seats.get(message.oldSeatIndex); + newSeat = rollingStockEntity.seats.get(message.newSeatIndex); + newSeat.addPassenger(playerEntity); + oldSeat.removePassenger(playerEntity); + playerEntity.mountEntity(newSeat); + if (ctx.side == Side.SERVER) { + TrainsInMotion.updateChannel.sendToAllAround(new PacketSeatUpdate(message.rollingStockId,message.playerId,message.oldSeatIndex,message.newSeatIndex), + new NetworkRegistry.TargetPoint(playerEntity.dimension,rollingStockEntity.posX,rollingStockEntity.posY,rollingStockEntity.posZ,256D)); + } + return null; + } + } +} diff --git a/src/main/java/ebf/tim/utility/ClientProxy.java b/src/main/java/ebf/tim/utility/ClientProxy.java index 997258973d..5884a00c02 100644 --- a/src/main/java/ebf/tim/utility/ClientProxy.java +++ b/src/main/java/ebf/tim/utility/ClientProxy.java @@ -119,6 +119,10 @@ public void adminGui(String datacsv){ Minecraft.getMinecraft().displayGuiScreen(new GUIAdminBook(datacsv)); } + @Override + public void seatGUI(EntityPlayer player, GenericRailTransport transport) { + Minecraft.getMinecraft().displayGuiScreen(new GUISeatManager(transport)); + } @Override public boolean isClient(){return true;} diff --git a/src/main/java/ebf/tim/utility/CommonProxy.java b/src/main/java/ebf/tim/utility/CommonProxy.java index 3a564a9b41..8dc5cf0e66 100644 --- a/src/main/java/ebf/tim/utility/CommonProxy.java +++ b/src/main/java/ebf/tim/utility/CommonProxy.java @@ -6,10 +6,12 @@ import ebf.tim.api.SkinRegistry; import ebf.tim.blocks.BlockDynamic; import ebf.tim.entities.GenericRailTransport; +import ebf.tim.gui.GUISeatManager; import ebf.tim.registry.TiMBlocks; import ebf.tim.registry.TiMFluids; import ebf.tim.registry.TiMItems; import ebf.tim.registry.TiMOres; +import net.minecraft.client.Minecraft; import net.minecraft.entity.Entity; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.server.MinecraftServer; @@ -70,6 +72,8 @@ public Object getServerGuiElement(int ID, EntityPlayer player, World world, int public void adminGui(String datacsv){} + public void seatGUI(EntityPlayer player, GenericRailTransport transport) { } + public boolean isClient(){return false;} /**