Skip to content

Commit

Permalink
fix: sync issue between screen and hidden explosive
Browse files Browse the repository at this point in the history
  • Loading branch information
fenn7 committed Sep 7, 2023
1 parent b5c22ba commit 83bb2d0
Show file tree
Hide file tree
Showing 8 changed files with 84 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@

import fenn7.grenadesandgadgets.commonside.GrenadesMod;
import net.fabricmc.fabric.api.client.screenhandler.v1.ScreenRegistry;
import net.fabricmc.fabric.api.screenhandler.v1.ExtendedScreenHandlerType;
import net.minecraft.screen.ScreenHandlerType;
import net.minecraft.util.Identifier;
import net.minecraft.util.registry.Registry;

public class GrenadesModScreens {
public static ScreenHandlerType<HiddenExplosiveScreenHandler> HIDDEN_EXPLOSIVE_SCREEN_HANDLER =
Registry.register(Registry.SCREEN_HANDLER, new Identifier(GrenadesMod.MOD_ID, "hidden_explosive"),
new ScreenHandlerType<>(HiddenExplosiveScreenHandler::new));
new ExtendedScreenHandlerType<>(HiddenExplosiveScreenHandler::new));

public static void registerScreens() {
GrenadesMod.LOGGER.warn("Initialising Grenades And Gadgets Screens...");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@

import com.mojang.blaze3d.systems.RenderSystem;
import fenn7.grenadesandgadgets.commonside.GrenadesMod;
import fenn7.grenadesandgadgets.commonside.item.network.GrenadesModC2SPackets;
import fenn7.grenadesandgadgets.commonside.util.GrenadesModUtil;
import net.fabricmc.fabric.api.client.networking.v1.ClientPlayNetworking;
import net.fabricmc.fabric.api.networking.v1.ServerPlayNetworking;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.gui.screen.ingame.HandledScreen;
import net.minecraft.client.gui.widget.ButtonWidget;
Expand Down Expand Up @@ -30,8 +33,8 @@ protected void init() {
this.titleX = (this.backgroundWidth - this.textRenderer.getWidth(this.title)) / 2;
this.addSelectableChild(new ButtonWidget(this.x + 124, this.y + 63, 14, 9, GrenadesModUtil.textOf(""),
widget -> {
MinecraftClient.getInstance().player.sendMessage(GrenadesModUtil.translatableTextOf(this.handler.hasGrenade() ? ARM_START : CANT_ARM), false);
this.handler.setDelegateValue(1, this.handler.hasGrenade() && !this.handler.isArming() ? 1 : 0);
ClientPlayNetworking.send(GrenadesModC2SPackets.SYNC_HIDDEN_EXPLOSIVE_C2S, GrenadesModUtil.createBuffer().writeBlockPos(this.handler.getBlockEntityPos()));
})
);
}
Expand Down Expand Up @@ -59,5 +62,6 @@ public void render(MatrixStack matrices, int mouseX, int mouseY, float delta) {
this.drawMouseoverTooltip(matrices, mouseX, mouseY);
this.textRenderer.draw(matrices, GrenadesModUtil.translatableTextOf(LEFT_TITLE), this.x + 10 + 2, this.y + 20, 0);
this.textRenderer.draw(matrices, GrenadesModUtil.translatableTextOf(RIGHT_TITLE), this.x + 10 + 88, this.y + 20, 0);
this.textRenderer.draw(matrices, Text.of(this.handler.getBlockEntityPos().toShortString()), 0, 0, 0);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,22 @@
import net.minecraft.inventory.Inventory;
import net.minecraft.inventory.SimpleInventory;
import net.minecraft.item.ItemStack;
import net.minecraft.network.PacketByteBuf;
import net.minecraft.screen.ArrayPropertyDelegate;
import net.minecraft.screen.PropertyDelegate;
import net.minecraft.screen.ScreenHandler;
import net.minecraft.screen.slot.Slot;
import net.minecraft.util.math.BlockPos;

public class HiddenExplosiveScreenHandler extends ScreenHandler {
private static final int PIXEL_BAR_LENGTH = 60;
private BlockPos blockEntityPos;
private final Inventory inventory;
private final PropertyDelegate delegate;

public HiddenExplosiveScreenHandler(int syncId, PlayerInventory playerInv) {
public HiddenExplosiveScreenHandler(int syncId, PlayerInventory playerInv, PacketByteBuf buf) {
this(syncId, playerInv, new SimpleInventory(1), new ArrayPropertyDelegate(2));
this.blockEntityPos = buf.readBlockPos();
}

public HiddenExplosiveScreenHandler(int syncId, PlayerInventory playerInv, Inventory inventory, PropertyDelegate delegate) {
Expand All @@ -29,6 +33,7 @@ public HiddenExplosiveScreenHandler(int syncId, PlayerInventory playerInv, Inven
this.inventory = inventory;
inventory.onOpen(playerInv.player);
this.delegate = delegate;
this.blockEntityPos = BlockPos.ORIGIN;

this.addSlot(new HiddenExplosiveGrenadeSlot(this.inventory, 0, 123, 34));

Expand All @@ -37,14 +42,17 @@ public HiddenExplosiveScreenHandler(int syncId, PlayerInventory playerInv, Inven
this.addProperties(delegate);
}

public BlockPos getBlockEntityPos() {
return this.blockEntityPos;
}

public void setDelegateValue(int index, int value) {
if (index >= 0 && index < this.delegate.size()) {
this.delegate.set(index, value);
}
}

public boolean isArming() {
GrenadesMod.LOGGER.warn("HANLDER THINKS ARM FLAG IS " + this.delegate.get(1));
return this.delegate.get(1) == 1;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import fenn7.grenadesandgadgets.commonside.damage.GrenadesModDamageSources;
import fenn7.grenadesandgadgets.commonside.entity.GrenadesModEntities;
import fenn7.grenadesandgadgets.commonside.item.GrenadesModItems;
import fenn7.grenadesandgadgets.commonside.item.network.GrenadesModC2SPackets;
import fenn7.grenadesandgadgets.commonside.item.recipe.GrenadesModSpecialRecipes;
import fenn7.grenadesandgadgets.commonside.status.GrenadesModStatus;
import fenn7.grenadesandgadgets.commonside.util.GrenadesModRegistries;
Expand Down Expand Up @@ -33,5 +34,6 @@ public void onInitialize() {
GrenadesModSpecialRecipes.registerRecipes();
GrenadesModStatus.registerEffects();
GrenadesModRegistries.registerRegistries();
GrenadesModC2SPackets.registerC2SPackets();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import fenn7.grenadesandgadgets.commonside.item.custom.block.HiddenExplosiveBlockItem;
import fenn7.grenadesandgadgets.commonside.util.GrenadesModUtil;
import fenn7.grenadesandgadgets.commonside.util.ImplementedInventory;
import net.fabricmc.fabric.api.screenhandler.v1.ExtendedScreenHandlerFactory;
import net.minecraft.block.BlockState;
import net.minecraft.block.entity.BlockEntity;
import net.minecraft.entity.player.PlayerEntity;
Expand All @@ -15,11 +16,13 @@
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NbtCompound;
import net.minecraft.network.Packet;
import net.minecraft.network.PacketByteBuf;
import net.minecraft.network.listener.ClientPlayPacketListener;
import net.minecraft.network.packet.s2c.play.BlockEntityUpdateS2CPacket;
import net.minecraft.screen.NamedScreenHandlerFactory;
import net.minecraft.screen.PropertyDelegate;
import net.minecraft.screen.ScreenHandler;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.text.Text;
import net.minecraft.util.collection.DefaultedList;
import net.minecraft.util.math.BlockPos;
Expand All @@ -34,7 +37,7 @@
import software.bernie.geckolib3.core.manager.AnimationData;
import software.bernie.geckolib3.core.manager.AnimationFactory;

public class HiddenExplosiveBlockEntity extends BlockEntity implements IAnimatable, NamedScreenHandlerFactory, ImplementedInventory {
public class HiddenExplosiveBlockEntity extends BlockEntity implements IAnimatable, ExtendedScreenHandlerFactory, ImplementedInventory {
public static final int MAX_ARMING_TICKS = 40;
private static final String TRANSLATABLE = "container.grenadesandgadgets.hidden_explosive";
private final DefaultedList<ItemStack> inventory = DefaultedList.ofSize(1, ItemStack.EMPTY);
Expand Down Expand Up @@ -75,8 +78,7 @@ public int size() {
}

public static void tick(World world, BlockPos pos, BlockState state, HiddenExplosiveBlockEntity entity) {
GrenadesMod.LOGGER.warn("ENTITY THINKS ARM FLAG IS " + entity.armingFlag);
if (/*entity.armingFlag == 1 &&*/ !entity.getStack(0).isEmpty()) {
if (entity.armingFlag == 1 && !entity.getStack(0).isEmpty()) {
if (entity.currentArmingTicks < MAX_ARMING_TICKS) {
++entity.currentArmingTicks;
if (entity.currentArmingTicks >= MAX_ARMING_TICKS) {
Expand All @@ -88,11 +90,17 @@ public static void tick(World world, BlockPos pos, BlockState state, HiddenExplo
}
}

public PropertyDelegate getDelegate() {
return this.delegate;
}

public void resetArming() {
if (this.currentArmingTicks > 0) {
--this.currentArmingTicks;
if (this.currentArmingTicks <= 0) {
this.armingFlag = 0;
}
};
this.armingFlag = 0;
}

public Item getDisguiseBlockItem() {
Expand Down Expand Up @@ -163,4 +171,9 @@ public Text getDisplayName() {
public ScreenHandler createMenu(int syncId, PlayerInventory inv, PlayerEntity player) {
return new HiddenExplosiveScreenHandler(syncId, inv, this, this.delegate);
}

@Override
public void writeScreenOpeningData(ServerPlayerEntity player, PacketByteBuf buf) {
buf.writeBlockPos(this.pos);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package fenn7.grenadesandgadgets.commonside.item.network;

import fenn7.grenadesandgadgets.commonside.GrenadesMod;
import fenn7.grenadesandgadgets.commonside.item.network.packets.SyncHiddenExplosiveC2SPacket;
import net.fabricmc.fabric.api.networking.v1.ServerPlayNetworking;
import net.minecraft.util.Identifier;

public class GrenadesModC2SPackets {
public static Identifier SYNC_HIDDEN_EXPLOSIVE_C2S = new Identifier(GrenadesMod.MOD_ID, "hidden_explosive_c2s");

public static void registerC2SPackets() {
GrenadesMod.LOGGER.warn("Initialising Grenades And Gadgets S2C Packets...");
ServerPlayNetworking.registerGlobalReceiver(SYNC_HIDDEN_EXPLOSIVE_C2S, SyncHiddenExplosiveC2SPacket::receive);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package fenn7.grenadesandgadgets.commonside.item.network.packets;

import fenn7.grenadesandgadgets.client.screen.HiddenExplosiveScreenHandler;
import fenn7.grenadesandgadgets.commonside.block.GrenadesModBlockEntities;
import fenn7.grenadesandgadgets.commonside.block.entity.HiddenExplosiveBlockEntity;
import net.fabricmc.fabric.api.networking.v1.PacketSender;
import net.minecraft.network.PacketByteBuf;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.network.ServerPlayNetworkHandler;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.util.math.BlockPos;

public class SyncHiddenExplosiveC2SPacket {
public static void receive(MinecraftServer server, ServerPlayerEntity serverPlayerEntity,
ServerPlayNetworkHandler serverPlayNetworkHandler, PacketByteBuf buf, PacketSender packetSender) {
BlockPos pos = buf.readBlockPos();
if (pos != null) {
var optional = serverPlayerEntity.getWorld().getBlockEntity(pos, GrenadesModBlockEntities.HIDDEN_EXPLOSIVE_BLOCK_ENTITY);
var handler = serverPlayerEntity.currentScreenHandler;
if (optional.isPresent()) {
optional.get().getDelegate().set(1, 1);
} else if (handler instanceof HiddenExplosiveScreenHandler hiddenHandler) {
hiddenHandler.setDelegateValue(1, 1);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,15 @@ static boolean areAnyBlocksBetween(World world, BlockPos start, BlockPos end) {
state -> state.getMaterial().isSolid())).getType() == HitResult.Type.BLOCK;
}

static PacketByteBuf createBuffer() {
return new PacketByteBuf(Unpooled.buffer());
}

static void addEffectServerAndClient(LivingEntity entity, StatusEffectInstance effect) {
entity.addStatusEffect(effect);
if (!entity.world.isClient) {
try {
PacketByteBuf buf = new PacketByteBuf(Unpooled.buffer());
PacketByteBuf buf = createBuffer();
buf.writeIntList(IntList.of(entity.getId(), StatusEffect.getRawId(effect.getEffectType()),
effect.getDuration(), effect.getAmplifier()));
ServerPlayerEntity player = (ServerPlayerEntity) entity.world.getPlayers().get(0);
Expand All @@ -95,7 +99,7 @@ static void removeEffectServerAndClient(LivingEntity entity, StatusEffect effect
if (entity.hasStatusEffect(effect)) {
if (!entity.world.isClient) {
try {
PacketByteBuf buf = new PacketByteBuf(Unpooled.buffer());
PacketByteBuf buf = createBuffer();
buf.writeIntList(IntList.of(entity.getId(), StatusEffect.getRawId(effect)));
ServerPlayerEntity player = (ServerPlayerEntity) entity.world.getPlayers().get(0);
ServerPlayNetworking.send(player, GrenadesModS2CPackets.REMOVE_EFFECT_S2C, buf);
Expand Down

0 comments on commit 83bb2d0

Please sign in to comment.