Skip to content

Commit

Permalink
Made drawbridge inventory re-open when adding/removing advanced upgrade
Browse files Browse the repository at this point in the history
Preserving cursor stack
  • Loading branch information
fuj1n committed Sep 23, 2019
1 parent 8631652 commit a4e4b79
Show file tree
Hide file tree
Showing 7 changed files with 125 additions and 4 deletions.
Expand Up @@ -11,14 +11,19 @@
import slimeknights.tmechworks.client.gui.components.ArrowWidget;
import slimeknights.tmechworks.common.inventory.DrawbridgeContainer;
import slimeknights.tmechworks.common.network.PacketHandler;
import slimeknights.tmechworks.common.network.UpdatePlaceDirectionPacket;
import slimeknights.tmechworks.common.network.packet.ServerReopenUiPacket;
import slimeknights.tmechworks.common.network.packet.UpdatePlaceDirectionPacket;
import slimeknights.tmechworks.library.Util;

public class DrawbridgeScreen extends ContainerScreen<DrawbridgeContainer> {
public static final ResourceLocation SCREEN_LOCATION = new ResourceLocation("tmechworks", "textures/gui/drawbridge.png");

private final boolean isAdvanced;

public DrawbridgeScreen(DrawbridgeContainer container, PlayerInventory inventory, ITextComponent name) {
super(container, inventory, name);

isAdvanced = container.getTile().stats.isAdvanced;
}

public static DrawbridgeScreen create(DrawbridgeContainer container, PlayerInventory player, ITextComponent title){
Expand All @@ -34,6 +39,16 @@ protected void init() {
addButton(arrow);
}

@Override
public void tick() {
super.tick();

// Reinitialize UI if the drawbridge type changes
if(isAdvanced != container.getTile().stats.isAdvanced) {
PacketHandler.send(PacketDistributor.SERVER.noArg(), new ServerReopenUiPacket(container.getTile().getPos()));
}
}

@Override
public void render(int mouseX, int mouseY, float partialTicks) {
this.renderBackground();
Expand Down
Expand Up @@ -60,7 +60,7 @@ protected void fillStateContainer(StateContainer.Builder<Block, BlockState> buil
builder.add(FACING);
}

protected boolean openGui(PlayerEntity player, World world, BlockPos pos) {
public boolean openGui(PlayerEntity player, World world, BlockPos pos) {
if (player instanceof ServerPlayerEntity && !(player instanceof FakePlayer)) {
TileEntity te = world.getTileEntity(pos);

Expand Down
Expand Up @@ -12,12 +12,15 @@
import slimeknights.tmechworks.common.inventory.slots.ValidatingSlot;

public class DrawbridgeContainer extends BaseContainer<DrawbridgeTileEntity> {
public final PlayerInventory playerInventory;

public DrawbridgeContainer(int id, PlayerInventory playerInventory, DrawbridgeTileEntity te) {
super(MechworksContent.Containers.drawbridge, id, te);

this.playerInventory = playerInventory;
te.openInventory(playerInventory.player);

addSlot(new ValidatingSlot(tile.slots, 0, 80, 36));
addDrawbridgeSlots();

for(int x = 0; x < 2; x++){
for(int y = 0; y < 2; y++){
Expand All @@ -28,6 +31,10 @@ public DrawbridgeContainer(int id, PlayerInventory playerInventory, DrawbridgeTi
addPlayerInventory(playerInventory, 8, 84);
}

protected void addDrawbridgeSlots(){
addSlot(new ValidatingSlot(tile.slots, 0, 80, 36));
}

@OnlyIn(Dist.CLIENT)
public static DrawbridgeContainer factory(int id, PlayerInventory playerInventory, PacketBuffer extraData) {
BlockPos pos = extraData.readBlockPos();
Expand Down
Expand Up @@ -3,6 +3,9 @@
import net.minecraftforge.fml.network.NetworkRegistry;
import net.minecraftforge.fml.network.PacketDistributor;
import net.minecraftforge.fml.network.simple.SimpleChannel;
import slimeknights.tmechworks.common.network.packet.ClientSetCursorStackPacket;
import slimeknights.tmechworks.common.network.packet.ServerReopenUiPacket;
import slimeknights.tmechworks.common.network.packet.UpdatePlaceDirectionPacket;
import slimeknights.tmechworks.library.Util;

public final class PacketHandler {
Expand All @@ -17,6 +20,8 @@ public static void register() {
int id = 0;

HANDLER.registerMessage(id++, UpdatePlaceDirectionPacket.class, UpdatePlaceDirectionPacket::encode, UpdatePlaceDirectionPacket::decode, UpdatePlaceDirectionPacket.Handler::handle);
HANDLER.registerMessage(id++, ServerReopenUiPacket.class, ServerReopenUiPacket::encode, ServerReopenUiPacket::decode, ServerReopenUiPacket.Handler::handle);
HANDLER.registerMessage(id++, ClientSetCursorStackPacket.class, ClientSetCursorStackPacket::encode, ClientSetCursorStackPacket::decode, ClientSetCursorStackPacket.Handler::handle);
}

public static <T> void send(PacketDistributor.PacketTarget target, T message) {
Expand Down
@@ -0,0 +1,41 @@
package slimeknights.tmechworks.common.network.packet;

import net.minecraft.block.BlockState;
import net.minecraft.client.Minecraft;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.ItemStack;
import net.minecraft.network.PacketBuffer;
import net.minecraft.util.math.BlockPos;
import net.minecraftforge.fml.network.NetworkEvent;
import slimeknights.tmechworks.common.blocks.RedstoneMachineBlock;

import java.util.function.Supplier;

public class ClientSetCursorStackPacket {
private ItemStack stack;

public ClientSetCursorStackPacket(ItemStack stack) {
this.stack = stack;
}

public static void encode(ClientSetCursorStackPacket msg, PacketBuffer buf) {
buf.writeItemStack(msg.stack);
}

public static ClientSetCursorStackPacket decode(PacketBuffer buf) {
ItemStack stack = buf.readItemStack();

return new ClientSetCursorStackPacket(stack);
}

public static class Handler {
public static void handle(final ClientSetCursorStackPacket msg, Supplier<NetworkEvent.Context> ctx) {
NetworkEvent.Context context = ctx.get();
PlayerEntity player = Minecraft.getInstance().player;

context.enqueueWork(() -> {
player.inventory.setItemStack(msg.stack);
});
}
}
}
@@ -0,0 +1,52 @@
package slimeknights.tmechworks.common.network.packet;

import net.minecraft.block.BlockState;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.entity.player.ServerPlayerEntity;
import net.minecraft.item.ItemStack;
import net.minecraft.network.PacketBuffer;
import net.minecraft.util.math.BlockPos;
import net.minecraftforge.fml.network.NetworkEvent;
import net.minecraftforge.fml.network.PacketDistributor;
import slimeknights.tmechworks.common.blocks.RedstoneMachineBlock;
import slimeknights.tmechworks.common.network.PacketHandler;

import java.util.function.Supplier;

public class ServerReopenUiPacket {
private BlockPos pos;

public ServerReopenUiPacket(BlockPos pos) {
this.pos = pos;
}

public static void encode(ServerReopenUiPacket msg, PacketBuffer buf) {
buf.writeBlockPos(msg.pos);
}

public static ServerReopenUiPacket decode(PacketBuffer buf) {
BlockPos pos = buf.readBlockPos();

return new ServerReopenUiPacket(pos);
}

public static class Handler {
public static void handle(final ServerReopenUiPacket msg, Supplier<NetworkEvent.Context> ctx) {
NetworkEvent.Context context = ctx.get();
PlayerEntity player = context.getSender();

context.enqueueWork(() -> {
BlockState block = player.getEntityWorld().getBlockState(msg.pos);

if(block.getBlock() instanceof RedstoneMachineBlock) {
ItemStack cursorStack = player.inventory.getItemStack();
player.inventory.setItemStack(ItemStack.EMPTY);
((RedstoneMachineBlock)block.getBlock()).openGui(player, player.getEntityWorld(), msg.pos);
player.inventory.setItemStack(cursorStack);

PacketHandler.send(PacketDistributor.PLAYER.with(() -> (ServerPlayerEntity) player), new ClientSetCursorStackPacket(cursorStack));
}
});
}
}
}
@@ -1,4 +1,4 @@
package slimeknights.tmechworks.common.network;
package slimeknights.tmechworks.common.network.packet;

import net.minecraft.client.Minecraft;
import net.minecraft.entity.player.PlayerEntity;
Expand All @@ -9,6 +9,7 @@
import net.minecraftforge.fml.network.NetworkEvent;
import net.minecraftforge.fml.network.PacketDistributor;
import slimeknights.tmechworks.common.blocks.tileentity.IPlaceDirection;
import slimeknights.tmechworks.common.network.PacketHandler;

import java.util.function.Supplier;

Expand Down

0 comments on commit a4e4b79

Please sign in to comment.