Skip to content

Commit

Permalink
Fix container syncing
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremiahwinsley committed Aug 30, 2021
1 parent 9271728 commit 208a7ca
Show file tree
Hide file tree
Showing 8 changed files with 59 additions and 54 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ org.gradle.daemon=false

group=net.permutated
mod_id=pylons
version=1.0.2
version=1.0.3
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,8 @@ public ActionResultType use(BlockState state, World world, BlockPos pos, PlayerE
if (!world.isClientSide) {
TileEntity tileEntity = world.getBlockEntity(pos);
if (tileEntity instanceof AbstractPylonTile) {
AbstractPylonTile pylonTile = (AbstractPylonTile) tileEntity;

INamedContainerProvider containerProvider = new INamedContainerProvider() {
@Override
public ITextComponent getDisplayName() {
Expand All @@ -141,13 +143,13 @@ public ITextComponent getDisplayName() {
@Override
public Container createMenu(int i, PlayerInventory playerInventory, PlayerEntity playerEntity) {
PacketBuffer buffer = new PacketBuffer(Unpooled.buffer());
return containerFactory().create(i, playerInventory, buffer.writeBlockPos(pos));
pylonTile.updateContainer(buffer);
return containerFactory().create(i, playerInventory, buffer);
}
};

AbstractPylonTile pylonTile = (AbstractPylonTile) tileEntity;
if (player.getUUID().equals(pylonTile.getOwner()) || player.hasPermissions(2)) {
NetworkHooks.openGui((ServerPlayerEntity) player, containerProvider, tileEntity.getBlockPos());
NetworkHooks.openGui((ServerPlayerEntity) player, containerProvider, pylonTile::updateContainer);
} else {
return ActionResultType.FAIL;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import net.minecraft.util.text.TextFormatting;
import net.minecraft.util.text.TranslationTextComponent;
import net.permutated.pylons.inventory.container.AbstractPylonContainer;
import net.permutated.pylons.util.Constants;
import net.permutated.pylons.util.ResourceUtil;
import net.permutated.pylons.util.TranslationKey;

Expand Down Expand Up @@ -46,10 +47,10 @@ protected void renderLabels(MatrixStack matrixStack, int mouseX, int mouseY) {
String owner = this.menu.getOwnerName();

ITextComponent component;
if (owner != null) {
component = translate("owner", owner);
} else {
if (owner.equals(Constants.UNKNOWN)) {
component = translate("noOwner").withStyle(TextFormatting.RED);
} else {
component = translate("owner", owner);
}
drawText(matrixStack, component, 24);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@

public abstract class AbstractPylonContainer extends Container {

protected final AbstractPylonTile tileEntity;
@Nullable // should only be accessed from server
private final AbstractPylonTile tileEntity;
protected final String ownerName;

protected AbstractPylonContainer(@Nullable ContainerType<?> containerType, int windowId, PlayerInventory playerInventory, PacketBuffer packetBuffer) {
super(containerType, windowId);
Expand All @@ -41,34 +43,40 @@ protected AbstractPylonContainer(@Nullable ContainerType<?> containerType, int w
});
}

int nameLength = packetBuffer.readInt();
ownerName = packetBuffer.readUtf(nameLength);

registerPlayerSlots(wrappedInventory);
}

protected abstract RegistryObject<Block> getBlock();

@Nullable
protected int getInventorySize() {
return AbstractPylonTile.SLOTS;
}

public String getOwnerName() {
return tileEntity.getOwnerName();
return ownerName;
}

@Override
public boolean stillValid(PlayerEntity playerEntity) {
World world = tileEntity.getLevel();

if (world != null) {
IWorldPosCallable callable = IWorldPosCallable.create(world, tileEntity.getBlockPos());
return stillValid(callable, playerEntity, getBlock().get());
} else {
return false;
if (tileEntity != null) {
World world = tileEntity.getLevel();
if (world != null) {
IWorldPosCallable callable = IWorldPosCallable.create(world, tileEntity.getBlockPos());
return stillValid(callable, playerEntity, getBlock().get());
}
}
return false;
}

@Override
public ItemStack quickMoveStack(PlayerEntity playerIn, int index) {
ItemStack itemstack = ItemStack.EMPTY;
Slot slot = this.slots.get(index);

int inventorySize = this.tileEntity.getInventorySize();
int inventorySize = getInventorySize();

if (slot != null && slot.hasItem()) {
ItemStack stack = slot.getItem();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@
import net.minecraft.network.PacketBuffer;
import net.minecraftforge.fml.RegistryObject;
import net.permutated.pylons.ModRegistry;
import net.permutated.pylons.tile.ExpulsionPylonTile;

public class ExpulsionPylonContainer extends AbstractPylonContainer {

private final boolean allowedDimension;

public ExpulsionPylonContainer(int windowId, PlayerInventory playerInventory, PacketBuffer packetBuffer) {
super(ModRegistry.EXPULSION_PYLON_CONTAINER.get(), windowId, playerInventory, packetBuffer);
allowedDimension = packetBuffer.readBoolean();
}

@Override
Expand All @@ -19,10 +21,6 @@ protected RegistryObject<Block> getBlock() {
}

public boolean isAllowedDimension() {
if (tileEntity instanceof ExpulsionPylonTile) {
ExpulsionPylonTile pylonTile = (ExpulsionPylonTile) tileEntity;
return pylonTile.isAllowedDimension();
}
return false;
return allowedDimension;
}
}
47 changes: 17 additions & 30 deletions src/main/java/net/permutated/pylons/tile/AbstractPylonTile.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.CompoundNBT;
import net.minecraft.network.NetworkManager;
import net.minecraft.network.PacketBuffer;
import net.minecraft.network.play.server.SUpdateTileEntityPacket;
import net.minecraft.tileentity.ITickableTileEntity;
import net.minecraft.tileentity.TileEntity;
Expand All @@ -21,6 +22,7 @@
import net.minecraftforge.items.IItemHandler;
import net.minecraftforge.items.ItemStackHandler;
import net.permutated.pylons.util.Constants;
import org.apache.commons.lang3.StringUtils;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
Expand Down Expand Up @@ -54,10 +56,6 @@ public <T> LazyOptional<T> getCapability(Capability<T> cap, @Nullable Direction
return super.getCapability(cap, side);
}

public int getInventorySize() {
return this.itemStackHandler.getSlots();
}

public void dropItems() {
AbstractPylonTile.dropItems(level, worldPosition, itemStackHandler);
}
Expand All @@ -81,11 +79,6 @@ public void setOwner(UUID owner) {
this.setChanged();
}

@Nullable
public String getOwnerName() {
return this.ownerName;
}

private long lastTicked = 0L;

public boolean canTick(final int every) {
Expand All @@ -108,6 +101,21 @@ protected static void dropItems(@Nullable World world, BlockPos pos, IItemHandle
}
}

/**
* Serialize data to be sent to the GUI on the client.
*
* Overrides MUST call the super method first to ensure correct deserialization.
* @param packetBuffer the packet ready to be filled
*/
public void updateContainer(PacketBuffer packetBuffer) {
String lastKnown = UsernameCache.getLastKnownUsername(owner);
String username = StringUtils.defaultString(lastKnown, Constants.UNKNOWN);

packetBuffer.writeBlockPos(worldPosition);
packetBuffer.writeInt(username.length());
packetBuffer.writeUtf(username);
}

// Save TE data to disk
@Override
public CompoundNBT save(CompoundNBT tag) {
Expand All @@ -123,17 +131,6 @@ private void writeOwner(CompoundNBT tag) {
}
}

// Write username to a provided CompoundNBT
// Only used for server -> client sync
private void writeUsername(CompoundNBT tag) {
if (owner != null) {
ownerName = UsernameCache.getLastKnownUsername(owner);
if (ownerName != null) {
tag.putString(Constants.NBT.NAME, ownerName);
}
}
}

// Load TE data from disk
@Override
public void load(BlockState state, CompoundNBT tag) {
Expand All @@ -149,27 +146,17 @@ private void readOwner(CompoundNBT tag) {
}
}

// Read username from a provided CompoundNBT
// Only used for server -> client sync
private void readUsername(CompoundNBT tag) {
if (tag.contains(Constants.NBT.NAME)) {
ownerName = tag.getString(Constants.NBT.NAME);
}
}

// Called whenever a client loads a new chunk
@Override
public CompoundNBT getUpdateTag() {
CompoundNBT tag = super.getUpdateTag();
writeOwner(tag);
writeUsername(tag);
return tag;
}

@Override
public void handleUpdateTag(BlockState state, CompoundNBT tag) {
readOwner(tag);
readUsername(tag);
}

// Called whenever a block update happens on the client
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import net.minecraft.entity.player.ServerPlayerEntity;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.CompoundNBT;
import net.minecraft.network.PacketBuffer;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.management.PlayerInteractionManager;
import net.minecraft.tags.BlockTags;
Expand Down Expand Up @@ -64,6 +65,12 @@ public void tick() {
}
}

@Override
public void updateContainer(PacketBuffer packetBuffer) {
super.updateContainer(packetBuffer);
packetBuffer.writeBoolean(isAllowedDimension());
}

public boolean isAllowedDimension() {
if (level != null) {
if (allowedDimensions == null) {
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/net/permutated/pylons/util/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ private Constants() {
public static final String INFUSION_PYLON = "infusion_pylon";


public static final String UNKNOWN = "unknown";

public static class NBT {
private NBT() {
// nothing to do
Expand Down

0 comments on commit 208a7ca

Please sign in to comment.