Skip to content

Commit

Permalink
Improve implementation of opening various Guis (#5566)
Browse files Browse the repository at this point in the history
* Pass selected hotbar slot when opening personal chest. Should fix #5557

* Remove telling the server to open a personal chest. Only the server should tell the client to open it

* Don't bother even registering the client to server packet for opening personal chest

* Use openGui for block variant of personal chest

* Make it so openGui can be used for Items as well as blocks

* Use new item openGui system for portable teleporter and seismic reader

* Add wrapper for opening Gui's of by Entity

* Only clear sounds on client

* Minor clean
  • Loading branch information
pupnewfster committed Jul 22, 2019
1 parent 9e26fea commit 50b53ae
Show file tree
Hide file tree
Showing 25 changed files with 277 additions and 443 deletions.
156 changes: 90 additions & 66 deletions src/main/java/mekanism/client/ClientProxy.java
Expand Up @@ -139,6 +139,7 @@
import mekanism.common.item.ItemBlockMachine;
import mekanism.common.item.ItemBlockTransmitter;
import mekanism.common.item.ItemCraftingFormula;
import mekanism.common.item.ItemDictionary;
import mekanism.common.item.ItemPortableTeleporter;
import mekanism.common.item.ItemSeismicReader;
import mekanism.common.item.ItemWalkieTalkie;
Expand Down Expand Up @@ -220,6 +221,7 @@
import net.minecraft.client.renderer.block.model.ModelResourceLocation;
import net.minecraft.client.renderer.block.statemap.IStateMapper;
import net.minecraft.client.renderer.entity.RenderSkeleton;
import net.minecraft.entity.Entity;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.EnumDyeColor;
import net.minecraft.item.Item;
Expand Down Expand Up @@ -276,24 +278,6 @@ public void loadConfiguration() {
}
}

@Override
public void openPersonalChest(EntityPlayer entityplayer, int id, int windowId, boolean isBlock, BlockPos pos, EnumHand hand) {
if (id == 0) {
if (isBlock) {
TileEntityPersonalChest tileEntity = (TileEntityPersonalChest) entityplayer.world.getTileEntity(pos);
FMLClientHandler.instance().displayGuiScreen(entityplayer, new GuiPersonalChest(entityplayer.inventory, tileEntity));
entityplayer.openContainer.windowId = windowId;
} else {
ItemStack stack = entityplayer.getHeldItem(hand);
if (MachineType.get(stack) == MachineType.PERSONAL_CHEST) {
InventoryPersonalChest inventory = new InventoryPersonalChest(stack, hand);
FMLClientHandler.instance().displayGuiScreen(entityplayer, new GuiPersonalChest(entityplayer.inventory, inventory));
entityplayer.openContainer.windowId = windowId;
}
}
}
}

@Override
public void registerTESRs() {
ClientRegistry.bindTileEntitySpecialRenderer(TileEntityAdvancedFactory.class, new RenderConfigurableMachine<>());
Expand Down Expand Up @@ -684,16 +668,94 @@ private String getProperties(List<String> entries) {
return properties.toString();
}

private GuiScreen getClientItemGui(EntityPlayer player, BlockPos pos) {
int currentItem = pos.getX();
int handOrdinal = pos.getY();
if (currentItem < 0 || currentItem >= player.inventory.mainInventory.size() || handOrdinal < 0 || handOrdinal >= EnumHand.values().length) {
//If it is out of bounds don't do anything
return null;
}
ItemStack stack = player.inventory.getStackInSlot(currentItem);
if (stack.isEmpty()) {
return null;
}
EnumHand hand = EnumHand.values()[handOrdinal];
int guiID = pos.getZ();
switch (guiID) {
case 0:
if (stack.getItem() instanceof ItemDictionary) {
return new GuiDictionary(player.inventory);
}
break;
case 14:
if (stack.getItem() instanceof ItemPortableTeleporter) {
return new GuiTeleporter(player, hand, stack);
}
break;
case 19:
if (MachineType.get(stack) == MachineType.PERSONAL_CHEST) {
//Ensure the item didn't change. From testing even if it did things still seemed to work properly but better safe than sorry
return new GuiPersonalChest(player.inventory, new InventoryPersonalChest(stack, hand));
}
break;
case 38:
if (stack.getItem() instanceof ItemSeismicReader) {
return new GuiSeismicReader(player.world, new Coord4D(player), stack.copy());
}
break;
}
return null;
}

private GuiScreen getClientEntityGui(EntityPlayer player, World world, BlockPos pos) {
int entityID = pos.getX();
Entity entity = world.getEntityByID(entityID);
if (entity == null) {
return null;
}
int guiID = pos.getY();
switch (guiID) {
case 21:
if (entity instanceof EntityRobit) {
return new GuiRobitMain(player.inventory, (EntityRobit) entity);
}
break;
case 22:
if (entity instanceof EntityRobit) {
return new GuiRobitCrafting(player.inventory, (EntityRobit) entity);
}
break;
case 23:
if (entity instanceof EntityRobit) {
return new GuiRobitInventory(player.inventory, (EntityRobit) entity);
}
break;
case 24:
if (entity instanceof EntityRobit) {
return new GuiRobitSmelting(player.inventory, (EntityRobit) entity);
}
break;
case 25:
if (entity instanceof EntityRobit) {
return new GuiRobitRepair(player.inventory, (EntityRobit) entity);
}
break;
}
return null;
}

@Override
@SuppressWarnings("unchecked")
public GuiScreen getClientGui(int ID, EntityPlayer player, World world, BlockPos pos) {
//TODO: Replace magic numbers here and in sub methods with static lookup ints
if (ID == 0) {
return getClientItemGui(player, pos);
} else if (ID == 1) {
return getClientEntityGui(player, world, pos);
}
TileEntity tileEntity = world.getTileEntity(pos);

switch (ID) {
case 0:
return new GuiDictionary(player.inventory);
case 1:
break; // Used to be credits UI
//0, 1 USED BEFORE SWITCH
case 2:
return new GuiDigitalMiner(player.inventory, (TileEntityDigitalMiner) tileEntity);
case 3:
Expand All @@ -718,12 +780,7 @@ public GuiScreen getClientGui(int ID, EntityPlayer player, World world, BlockPos
return new GuiMetallurgicInfuser(player.inventory, (TileEntityMetallurgicInfuser) tileEntity);
case 13:
return new GuiTeleporter(player.inventory, (TileEntityTeleporter) tileEntity);
case 14:
ItemStack itemStack = player.getHeldItem(EnumHand.values()[pos.getX()]);
if (!itemStack.isEmpty() && itemStack.getItem() instanceof ItemPortableTeleporter) {
return new GuiTeleporter(player, EnumHand.values()[pos.getX()], itemStack);
}
return null;
//EMPTY 14
case 15:
return new GuiPurificationChamber(player.inventory, (TileEntityAdvancedElectricMachine<PurificationRecipe>) tileEntity);
case 16:
Expand All @@ -732,37 +789,9 @@ public GuiScreen getClientGui(int ID, EntityPlayer player, World world, BlockPos
return new GuiElectricPump(player.inventory, (TileEntityElectricPump) tileEntity);
case 18:
return new GuiDynamicTank(player.inventory, (TileEntityDynamicTank) tileEntity);
//EMPTY 19, 20
case 21:
EntityRobit robit = (EntityRobit) world.getEntityByID(pos.getX());
if (robit != null) {
return new GuiRobitMain(player.inventory, robit);
}
return null;
case 22:
robit = (EntityRobit) world.getEntityByID(pos.getX());
if (robit != null) {
return new GuiRobitCrafting(player.inventory, robit);
}
return null;
case 23:
robit = (EntityRobit) world.getEntityByID(pos.getX());
if (robit != null) {
return new GuiRobitInventory(player.inventory, robit);
}
return null;
case 24:
robit = (EntityRobit) world.getEntityByID(pos.getX());
if (robit != null) {
return new GuiRobitSmelting(player.inventory, robit);
}
return null;
case 25:
robit = (EntityRobit) world.getEntityByID(pos.getX());
if (robit != null) {
return new GuiRobitRepair(player.inventory, robit);
}
return null;
case 19:
return new GuiPersonalChest(player.inventory, (TileEntityPersonalChest) tileEntity);
//EMPTY 20, 21, 22, 23, 24, 25
case 29:
return new GuiChemicalOxidizer(player.inventory, (TileEntityChemicalOxidizer) tileEntity);
case 30:
Expand All @@ -781,12 +810,7 @@ public GuiScreen getClientGui(int ID, EntityPlayer player, World world, BlockPos
return new GuiChemicalWasher(player.inventory, (TileEntityChemicalWasher) tileEntity);
case 37:
return new GuiChemicalCrystallizer(player.inventory, (TileEntityChemicalCrystallizer) tileEntity);
case 38:
ItemStack itemStack1 = player.getHeldItem(EnumHand.values()[pos.getX()]);
if (!itemStack1.isEmpty() && itemStack1.getItem() instanceof ItemSeismicReader) {
return new GuiSeismicReader(world, new Coord4D(player), itemStack1.copy());
}
return null;
//EMPTY 38
case 39:
return new GuiSeismicVibrator(player.inventory, (TileEntitySeismicVibrator) tileEntity);
case 40:
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/mekanism/client/gui/GuiPersonalChest.java
Expand Up @@ -16,14 +16,14 @@
public class GuiPersonalChest extends GuiMekanismTile<TileEntityPersonalChest> {

public GuiPersonalChest(InventoryPlayer inventory, TileEntityPersonalChest tile) {
super(tile, new ContainerPersonalChest(inventory, tile, null, true));
super(tile, new ContainerPersonalChest(inventory, tile));
xSize += 26;
ySize += 64;
addGuiElement(new GuiSecurityTab(this, tileEntity, getGuiLocation()));
}

public GuiPersonalChest(InventoryPlayer inventory, InventoryPersonalChest inv) {
super(null, new ContainerPersonalChest(inventory, null, inv, false));
super(null, new ContainerPersonalChest(inventory, inv));
xSize += 26;
ySize += 64;
addGuiElement(new GuiSecurityTab(this, getGuiLocation(), inv.currentHand));
Expand Down
22 changes: 7 additions & 15 deletions src/main/java/mekanism/client/gui/robit/GuiRobit.java
Expand Up @@ -3,10 +3,7 @@
import java.io.IOException;
import mekanism.client.gui.GuiMekanism;
import mekanism.client.gui.button.GuiButtonDisableableImage;
import mekanism.common.Mekanism;
import mekanism.common.entity.EntityRobit;
import mekanism.common.network.PacketRobit;
import mekanism.common.network.PacketRobit.RobitMessage;
import mekanism.common.util.MekanismUtils;
import mekanism.common.util.MekanismUtils.ResourceType;
import net.minecraft.client.gui.GuiButton;
Expand Down Expand Up @@ -45,25 +42,20 @@ public void initGui() {
@Override
protected void actionPerformed(GuiButton guibutton) throws IOException {
super.actionPerformed(guibutton);
if (!openGui(guibutton.id)) {
if (!shouldOpenGui(guibutton.id)) {
//Don't do anything when the button is the same one as the one we are on
return;
}
if (guibutton.id == mainButton.id) {
Mekanism.packetHandler.sendToServer(new RobitMessage(PacketRobit.RobitPacketType.GUI, 0, robit.getEntityId(), null));
mc.player.openGui(Mekanism.instance, 21, mc.world, robit.getEntityId(), 0, 0);
MekanismUtils.openEntityGui(mc.player, robit, 21);
} else if (guibutton.id == craftingButton.id) {
Mekanism.packetHandler.sendToServer(new RobitMessage(PacketRobit.RobitPacketType.GUI, 1, robit.getEntityId(), null));
mc.player.openGui(Mekanism.instance, 22, mc.world, robit.getEntityId(), 0, 0);
MekanismUtils.openEntityGui(mc.player, robit, 22);
} else if (guibutton.id == inventoryButton.id) {
Mekanism.packetHandler.sendToServer(new RobitMessage(PacketRobit.RobitPacketType.GUI, 2, robit.getEntityId(), null));
mc.player.openGui(Mekanism.instance, 23, mc.world, robit.getEntityId(), 0, 0);
MekanismUtils.openEntityGui(mc.player, robit, 23);
} else if (guibutton.id == smeltingButton.id) {
Mekanism.packetHandler.sendToServer(new RobitMessage(PacketRobit.RobitPacketType.GUI, 3, robit.getEntityId(), null));
mc.player.openGui(Mekanism.instance, 24, mc.world, robit.getEntityId(), 0, 0);
MekanismUtils.openEntityGui(mc.player, robit, 24);
} else if (guibutton.id == repairButton.id) {
Mekanism.packetHandler.sendToServer(new RobitMessage(PacketRobit.RobitPacketType.GUI, 4, robit.getEntityId(), null));
mc.player.openGui(Mekanism.instance, 25, mc.world, robit.getEntityId(), 0, 0);
MekanismUtils.openEntityGui(mc.player, robit, 25);
}
}

Expand All @@ -74,5 +66,5 @@ protected ResourceLocation getGuiLocation() {

protected abstract String getBackgroundImage();

protected abstract boolean openGui(int id);
protected abstract boolean shouldOpenGui(int id);
}
Expand Up @@ -27,7 +27,7 @@ protected String getBackgroundImage() {
}

@Override
protected boolean openGui(int id) {
protected boolean shouldOpenGui(int id) {
return id != 1;
}
}
Expand Up @@ -27,7 +27,7 @@ protected String getBackgroundImage() {
}

@Override
protected boolean openGui(int id) {
protected boolean shouldOpenGui(int id) {
return id != 2;
}
}
20 changes: 8 additions & 12 deletions src/main/java/mekanism/client/gui/robit/GuiRobitMain.java
Expand Up @@ -52,7 +52,7 @@ private void toggleNameChange() {

private void changeName() {
if (!nameChangeField.getText().isEmpty()) {
Mekanism.packetHandler.sendToServer(new RobitMessage(RobitPacketType.NAME, robit.getEntityId(), 0, nameChangeField.getText()));
Mekanism.packetHandler.sendToServer(new RobitMessage(RobitPacketType.NAME, robit.getEntityId(), nameChangeField.getText()));
toggleNameChange();
nameChangeField.setText("");
}
Expand All @@ -63,28 +63,24 @@ protected void actionPerformed(GuiButton guibutton) {
if (guibutton.id == confirmName.id) {
changeName();
} else if (guibutton.id == teleportHomeButton.id) {
Mekanism.packetHandler.sendToServer(new RobitMessage(RobitPacketType.GO_HOME, robit.getEntityId(), 0, null));
Mekanism.packetHandler.sendToServer(new RobitMessage(RobitPacketType.GO_HOME, robit.getEntityId(), null));
mc.displayGuiScreen(null);
} else if (guibutton.id == pickupButton.id) {
Mekanism.packetHandler.sendToServer(new RobitMessage(RobitPacketType.DROP_PICKUP, robit.getEntityId(), 0, null));
Mekanism.packetHandler.sendToServer(new RobitMessage(RobitPacketType.DROP_PICKUP, robit.getEntityId(), null));
} else if (guibutton.id == renameButton.id) {
toggleNameChange();
} else if (guibutton.id == followButton.id) {
Mekanism.packetHandler.sendToServer(new RobitMessage(RobitPacketType.FOLLOW, robit.getEntityId(), 0, null));
Mekanism.packetHandler.sendToServer(new RobitMessage(RobitPacketType.FOLLOW, robit.getEntityId(), null));
} else if (guibutton.id == mainButton.id) {
//Clicking main button doesn't do anything while already on the main GUI
} else if (guibutton.id == craftingButton.id) {
Mekanism.packetHandler.sendToServer(new RobitMessage(RobitPacketType.GUI, 1, robit.getEntityId(), null));
mc.player.openGui(Mekanism.instance, 22, mc.world, robit.getEntityId(), 0, 0);
MekanismUtils.openEntityGui(mc.player, robit, 22);
} else if (guibutton.id == inventoryButton.id) {
Mekanism.packetHandler.sendToServer(new RobitMessage(RobitPacketType.GUI, 2, robit.getEntityId(), null));
mc.player.openGui(Mekanism.instance, 23, mc.world, robit.getEntityId(), 0, 0);
MekanismUtils.openEntityGui(mc.player, robit, 23);
} else if (guibutton.id == smeltingButton.id) {
Mekanism.packetHandler.sendToServer(new RobitMessage(RobitPacketType.GUI, 3, robit.getEntityId(), null));
mc.player.openGui(Mekanism.instance, 24, mc.world, robit.getEntityId(), 0, 0);
MekanismUtils.openEntityGui(mc.player, robit, 24);
} else if (guibutton.id == repairButton.id) {
Mekanism.packetHandler.sendToServer(new RobitMessage(RobitPacketType.GUI, 4, robit.getEntityId(), null));
mc.player.openGui(Mekanism.instance, 25, mc.world, robit.getEntityId(), 0, 0);
MekanismUtils.openEntityGui(mc.player, robit, 25);
}
}

Expand Down
Expand Up @@ -112,7 +112,7 @@ protected void mouseClicked(int mouseX, int mouseY, int button) throws IOExcepti
}

@Override
protected boolean openGui(int id) {
protected boolean shouldOpenGui(int id) {
return id != 4;
}

Expand Down
Expand Up @@ -39,7 +39,7 @@ protected void drawGuiContainerBackgroundLayer(int xAxis, int yAxis) {
}

@Override
protected boolean openGui(int id) {
protected boolean shouldOpenGui(int id) {
return id != 3;
}

Expand Down

0 comments on commit 50b53ae

Please sign in to comment.