Skip to content

Commit

Permalink
Fixed buttons in multiplayer
Browse files Browse the repository at this point in the history
  • Loading branch information
kill05 committed Jun 17, 2024
1 parent 786f4fa commit 220e92a
Show file tree
Hide file tree
Showing 14 changed files with 68 additions and 133 deletions.
Binary file modified libraries/halplibe-4.1.1.jar
Binary file not shown.
19 changes: 10 additions & 9 deletions src/main/java/com/github/kill05/ArchitectGuis.java
Original file line number Diff line number Diff line change
@@ -1,52 +1,53 @@
package com.github.kill05;

import com.github.kill05.blocks.architectstation.ArchitectTableTileEntity;
import com.github.kill05.blocks.architectstation.inventory.ArchitectStationGuiFactory;
import com.github.kill05.blocks.architectstation.inventory.part.PartModeContainer;
import com.github.kill05.blocks.architectstation.inventory.part.PartModeGui;
import com.github.kill05.blocks.architectstation.inventory.tool.ToolModeContainer;
import com.github.kill05.blocks.architectstation.inventory.tool.ToolModeGui;
import com.github.kill05.packet.PacketSetTableOutput;
import net.minecraft.client.entity.player.EntityPlayerSP;
import net.minecraft.client.gui.GuiScreen;
import net.minecraft.core.entity.player.EntityPlayer;
import net.minecraft.core.player.inventory.Container;
import net.minecraft.server.entity.player.EntityPlayerMP;
import org.jetbrains.annotations.NotNull;
import turniplabs.halplibe.helper.gui.GuiHelper;
import turniplabs.halplibe.helper.gui.factory.block.TileGuiFactory;
import turniplabs.halplibe.helper.gui.registered.RegisteredGui;

public class ArchitectGuis {

public static final RegisteredGui PART_MODE = GuiHelper.registerServerGui(ArchitectTools.MOD_ID, "part_mode", new TileGuiFactory<ArchitectTableTileEntity>() {
public static final RegisteredGui PART_MODE = GuiHelper.registerServerGui(ArchitectTools.MOD_ID, "part_mode", new ArchitectStationGuiFactory() {
@Override
public @NotNull GuiScreen createGui(@NotNull RegisteredGui gui, @NotNull EntityPlayerSP player, @NotNull ArchitectTableTileEntity tile) {
return new PartModeGui(tile, player);
}

@Override
public @NotNull Container createContainer(@NotNull RegisteredGui gui, @NotNull EntityPlayerMP player, @NotNull ArchitectTableTileEntity tile) {
player.playerNetServerHandler.sendPacket(new PacketSetTableOutput(tile.getSelectedPart()));
return new PartModeContainer(tile, player);
}

@Override
public void onButtonPress(@NotNull RegisteredGui gui, @NotNull EntityPlayer player, int buttonId) {

public RegisteredGui getNextGui() {
return TOOL_MODE;
}
});

public static final RegisteredGui TOOL_MODE = GuiHelper.registerServerGui(ArchitectTools.MOD_ID, "tool_mode", new TileGuiFactory<ArchitectTableTileEntity>() {
public static final RegisteredGui TOOL_MODE = GuiHelper.registerServerGui(ArchitectTools.MOD_ID, "tool_mode", new ArchitectStationGuiFactory() {
@Override
public @NotNull GuiScreen createGui(@NotNull RegisteredGui gui, @NotNull EntityPlayerSP player, @NotNull ArchitectTableTileEntity tile) {
return new ToolModeGui(tile, player);
}

@Override
public @NotNull Container createContainer(@NotNull RegisteredGui gui, @NotNull EntityPlayerMP player, @NotNull ArchitectTableTileEntity tile) {
player.playerNetServerHandler.sendPacket(new PacketSetTableOutput(tile.getSelectedTool()));
return new ToolModeContainer(tile, player);
}

@Override
public RegisteredGui getNextGui() {
return PART_MODE;
}
});

}
2 changes: 0 additions & 2 deletions src/main/java/com/github/kill05/ArchitectTools.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import com.github.kill05.items.tool.ToolPartInfo;
import com.github.kill05.materials.ArchitectMaterial;
import com.github.kill05.materials.MaterialInfo;
import com.github.kill05.packet.PacketSetTableOutput;
import com.github.kill05.utils.ClassUtils;
import com.github.kill05.utils.ItemUtils;
import com.mojang.nbt.ListTag;
Expand Down Expand Up @@ -250,7 +249,6 @@ public void onInitialize() {
}
}

NetworkHelper.register(PacketSetTableOutput.class, true, true);
EntityHelper.createTileEntity(ArchitectTableTileEntity.class, "architect_station");

LOGGER.info("Architect's Tools initialized.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import net.minecraft.core.block.material.Material;
import net.minecraft.core.entity.player.EntityPlayer;
import net.minecraft.core.world.World;
import net.minecraft.server.entity.player.EntityPlayerMP;

public class ArchitectTableBlock extends BlockTileEntity {

Expand All @@ -25,14 +26,17 @@ public boolean blockActivated(World world, int x, int y, int z, EntityPlayer pla
if(player.isSneaking()) return false;

ArchitectGuis.PART_MODE.open(player, x, y, z);
if(player instanceof EntityPlayerMP playerMP) {
//playerMP.playerNetServerHandler.sendPacket(new Packet140TileEntityData(world.getBlockTileEntity(x, y, z)));
}

return true;
}


@Override
public void onBlockRemoved(World world, int x, int y, int z, int data) {
ArchitectTableTileEntity tile = ((ArchitectTableTileEntity) world.getBlockTileEntity(x, y, z));
ArchitectTableTileEntity tile = (ArchitectTableTileEntity) world.getBlockTileEntity(x, y, z);
InventoryUtils.dropInventoryContents(tile.getPartInventory(), world, x, y, z);
InventoryUtils.dropInventoryContents(tile.getToolInventory(), world, x, y, z);
super.onBlockRemoved(world, x, y, z, data);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public void drawButton(Minecraft mc, int mouseX, int mouseY) {
);
}

RenderUtils.getItemModel(item.asItem()).renderItemIntoGui(
RenderUtils.getItemModel(item).renderItemIntoGui(
Tessellator.instance, mc.fontRenderer, mc.renderEngine,
item.getDefaultStack(),
xPosition, //+ item.getButtonRenderOffsetX(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.github.kill05.utils.InventoryUtils;
import com.mojang.nbt.CompoundTag;
import net.minecraft.core.block.entity.TileEntity;
import net.minecraft.core.net.packet.Packet;
import net.minecraft.core.player.inventory.IInventory;
import net.minecraft.core.player.inventory.InventoryBasic;
import org.jetbrains.annotations.NotNull;
Expand Down Expand Up @@ -33,6 +34,8 @@ public void readFromNBT(CompoundTag tag) {

InventoryUtils.loadInventory(partInventory, tag.getList(PART_MODE));
InventoryUtils.loadInventory(toolInventory, tag.getList(TOOL_MODE));
this.selectedPart = ArchitectPart.fromIndex(tag.getInteger("selected_part"));
this.selectedTool = ArchitectTool.fromIndex(tag.getInteger("selected_tool"));
}

@Override
Expand All @@ -41,8 +44,14 @@ public void writeToNBT(CompoundTag tag) {

tag.putList(PART_MODE, InventoryUtils.writeInventory(partInventory));
tag.putList(TOOL_MODE, InventoryUtils.writeInventory(toolInventory));
tag.putInt("selected_part", selectedPart != null ? selectedPart.ordinal() : -1);
tag.putInt("selected_tool", selectedTool != null ? selectedTool.ordinal() : -1);
}

@Override
public Packet getDescriptionPacket() {
return null; //new Packet140TileEntityData(this);
}

public @NotNull IInventory getPartInventory() {
return partInventory;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,10 @@
import com.github.kill05.inventory.container.TileContainer;
import com.github.kill05.inventory.gui.TileContainerGui;
import com.github.kill05.items.IArchitectItem;
import com.github.kill05.packet.PacketSetTableOutput;
import net.minecraft.client.entity.player.EntityClientPlayerMP;
import net.minecraft.client.gui.GuiButton;
import net.minecraft.core.entity.player.EntityPlayer;
import net.minecraft.core.item.Item;
import net.minecraft.core.item.ItemStack;
import turniplabs.halplibe.helper.gui.registered.RegisteredGui;

public abstract class ArchitectStationGui<T extends IArchitectItem> extends TileContainerGui {

Expand All @@ -38,24 +35,13 @@ public void init() {
int y = (i / 4) * 18 + 18;
i++;

add(new ArchitectTableButton(tile, item, x, y)).setListener(button -> {
container.setSelected(item);

// Client to server (tell the server a client has changed the selected part/tool)
// The check is required in case the client is playing in singleplayer
if(player instanceof EntityClientPlayerMP playerMP) {
playerMP.sendQueue.addToSendQueue(new PacketSetTableOutput(container.getSelected()));
}
});
add(new ArchitectTableButton(tile, item, x, y));
}

GuiButton button = new ItemTexturedButton(new ItemStack(Item.ammoArrow), 0, 0);
button.setListener(aButton -> getNextGui().open(player, tile.x, tile.y, tile.z));
addAlignedButton(button, 7.5f, 6f);
}

public abstract RegisteredGui getNextGui();

@SuppressWarnings("unchecked")
@Override
public ArchitectStationContainer<T> getContainer() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.github.kill05.blocks.architectstation.inventory;

import com.github.kill05.blocks.architectstation.ArchitectTableTileEntity;
import com.github.kill05.items.IArchitectItem;
import net.minecraft.core.entity.player.EntityPlayer;
import org.jetbrains.annotations.NotNull;
import turniplabs.halplibe.helper.gui.factory.block.TileGuiFactory;
import turniplabs.halplibe.helper.gui.registered.RegisteredGui;

public interface ArchitectStationGuiFactory extends TileGuiFactory<ArchitectTableTileEntity> {

RegisteredGui getNextGui();

@Override
default void onButtonClick(@NotNull RegisteredGui gui, @NotNull EntityPlayer player, @NotNull EntityPlayer clicker, int buttonId) {
if(!(player.craftingInventory instanceof ArchitectStationContainer<?> container)) return;

if(buttonId == container.getModeValues().size()) {
if(player != clicker) return;
getNextGui().open(player, container.getTile());
return;
}

setSelected(container, buttonId);
}

private static <T extends IArchitectItem> void setSelected(ArchitectStationContainer<T> container, int buttonId) {
boolean inBounds = buttonId >= 0 && buttonId < container.getModeValues().size();
container.setSelected(inBounds ? container.getModeValues().get(buttonId) : null);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.github.kill05.blocks.architectstation.inventory.part;

import com.github.kill05.ArchitectGuis;
import com.github.kill05.ArchitectTools;
import com.github.kill05.blocks.architectstation.ArchitectTableTileEntity;
import com.github.kill05.blocks.architectstation.inventory.ArchitectStationGui;
Expand All @@ -15,7 +14,6 @@
import net.minecraft.core.item.ItemStack;
import net.minecraft.core.player.inventory.IInventory;
import org.lwjgl.opengl.GL11;
import turniplabs.halplibe.helper.gui.registered.RegisteredGui;

public class PartModeGui extends ArchitectStationGui<ArchitectPart> {

Expand Down Expand Up @@ -58,9 +56,4 @@ protected void drawGuiContainerForegroundLayer() {
float materialCost = part != null ? ArchitectMaterial.getDisplayMaterialValue(part.getMaterialCost()) : 0;
fontRenderer.drawCenteredString(amount + "/" + materialCost, 151, 83, materialCost > amount ? 0x00c00000 : 0x0000c000);
}

@Override
public RegisteredGui getNextGui() {
return ArchitectGuis.TOOL_MODE;
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.github.kill05.blocks.architectstation.inventory.tool;

import com.github.kill05.ArchitectGuis;
import com.github.kill05.blocks.architectstation.ArchitectTableTileEntity;
import com.github.kill05.blocks.architectstation.inventory.ArchitectStationGui;
import com.github.kill05.inventory.container.TileContainer;
Expand All @@ -9,7 +8,6 @@
import com.github.kill05.utils.RenderUtils;
import net.minecraft.core.entity.player.EntityPlayer;
import org.lwjgl.opengl.GL11;
import turniplabs.halplibe.helper.gui.registered.RegisteredGui;

public class ToolModeGui extends ArchitectStationGui<ArchitectTool> {

Expand Down Expand Up @@ -40,9 +38,4 @@ protected void drawGuiContainerBackgroundLayer(float f) {
);
}
}

@Override
public RegisteredGui getNextGui() {
return ArchitectGuis.PART_MODE;
}
}
6 changes: 6 additions & 0 deletions src/main/java/com/github/kill05/items/part/ArchitectPart.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,12 @@ public static ArchitectPart partItem(String id, String texture, float materialCo
.build(new ArchitectPart(id, ArchitectMaterial.getActualMaterialValue(materialCost)));
}

public static ArchitectPart fromIndex(int index) {
if(index < 0 || index >= VALUES.size()) return null;
return VALUES.get(index);
}


private final String partId;
private final int ordinal;
private final int materialCost;
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/com/github/kill05/items/tool/ArchitectTool.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ public static ArchitectTool toolItem(ArchitectTool tool) {
.build(tool);
}

public static ArchitectTool fromIndex(int index) {
if(index < 0 || index >= VALUES.size()) return null;
return VALUES.get(index);
}

private final String toolId;
private final int ordinal;
private final Map<PartStatistic<?>, Float> validStatistics; //value is multiplier
Expand Down
69 changes: 0 additions & 69 deletions src/main/java/com/github/kill05/packet/PacketSetTableOutput.java

This file was deleted.

22 changes: 0 additions & 22 deletions src/main/java/com/github/kill05/utils/PlayerUtils.java

This file was deleted.

0 comments on commit 220e92a

Please sign in to comment.