Skip to content

Commit

Permalink
Simple seat GUI stage 1
Browse files Browse the repository at this point in the history
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
  • Loading branch information
broscolotos committed Sep 8, 2023
1 parent 12916d6 commit 8248101
Show file tree
Hide file tree
Showing 6 changed files with 290 additions and 7 deletions.
2 changes: 2 additions & 0 deletions src/main/java/ebf/tim/TrainsInMotion.java
Original file line number Diff line number Diff line change
Expand Up @@ -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");


Expand Down
179 changes: 179 additions & 0 deletions src/main/java/ebf/tim/gui/GUISeatManager.java
Original file line number Diff line number Diff line change
@@ -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.*;


/**
* <h1>Transport GUI</h1>
* 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<Integer> 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;i<entity.getRiderOffsets().length; i++) {
if (percentTop(25)+(28*activeRow+1)>percentTop(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<entity.getRiderOffsets().length; i++) {
if (i/(rows+1) >=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<entity.getRiderOffsets().length; i++) {
if (i/(rows+1) >=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);
}
}
34 changes: 27 additions & 7 deletions src/main/java/ebf/tim/gui/GUITransport.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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<transport.getRiderOffsets().length; i++) {
if (i/(rows+1) >=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;i<transport.getRiderOffsets().length; i++) {
Expand All @@ -409,13 +429,13 @@ private void renderPassengerInventory(Minecraft mc){
}
if (i==0 && transport.riddenByEntity instanceof AbstractClientPlayer){
mc.getTextureManager().bindTexture(((AbstractClientPlayer) transport.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);
} else if (i>0 && 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);
}

/**
Expand Down
74 changes: 74 additions & 0 deletions src/main/java/ebf/tim/networking/PacketSeatUpdate.java
Original file line number Diff line number Diff line change
@@ -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;
/**
* <h1>Seat Packet</h1>
* 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<PacketSeatUpdate,IMessage> {
@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;
}
}
}
4 changes: 4 additions & 0 deletions src/main/java/ebf/tim/utility/ClientProxy.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;}

Expand Down
4 changes: 4 additions & 0 deletions src/main/java/ebf/tim/utility/CommonProxy.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;}

/**
Expand Down

0 comments on commit 8248101

Please sign in to comment.