From 24dcb5383a295d58ae870791e8fe34bea64bfad5 Mon Sep 17 00:00:00 2001 From: Snownee Date: Sat, 12 Dec 2020 19:32:32 +0800 Subject: [PATCH] Make the zapper selection logic server-side (#515) --- .../zapper/ZapperInteractionHandler.java | 22 +++---------- .../simibubi/create/events/ClientEvents.java | 12 +++++++ .../simibubi/create/events/CommonEvents.java | 11 +++++++ .../foundation/networking/AllPackets.java | 1 + .../networking/LeftClickPacket.java | 32 +++++++++++++++++++ 5 files changed, 61 insertions(+), 17 deletions(-) create mode 100644 src/main/java/com/simibubi/create/foundation/networking/LeftClickPacket.java diff --git a/src/main/java/com/simibubi/create/content/curiosities/zapper/ZapperInteractionHandler.java b/src/main/java/com/simibubi/create/content/curiosities/zapper/ZapperInteractionHandler.java index edd0e003f..030f9ee44 100644 --- a/src/main/java/com/simibubi/create/content/curiosities/zapper/ZapperInteractionHandler.java +++ b/src/main/java/com/simibubi/create/content/curiosities/zapper/ZapperInteractionHandler.java @@ -4,8 +4,6 @@ import java.util.Objects; import com.simibubi.create.AllSoundEvents; import com.simibubi.create.AllTags.AllBlockTags; -import com.simibubi.create.foundation.networking.AllPackets; -import com.simibubi.create.foundation.networking.NbtPacket; import com.simibubi.create.foundation.utility.BlockHelper; import net.minecraft.block.BlockState; @@ -17,7 +15,6 @@ import net.minecraft.state.properties.BlockStateProperties; import net.minecraft.state.properties.StairsShape; import net.minecraft.tileentity.TileEntity; import net.minecraft.util.ActionResultType; -import net.minecraft.util.Hand; import net.minecraft.util.SoundCategory; import net.minecraft.util.math.BlockPos; import net.minecraft.util.math.BlockRayTraceResult; @@ -25,30 +22,21 @@ import net.minecraft.util.math.RayTraceContext; import net.minecraft.util.math.RayTraceContext.BlockMode; import net.minecraft.util.math.RayTraceContext.FluidMode; import net.minecraft.util.math.Vec3d; -import net.minecraftforge.api.distmarker.Dist; import net.minecraftforge.event.entity.player.PlayerInteractEvent; import net.minecraftforge.eventbus.api.SubscribeEvent; import net.minecraftforge.fml.common.Mod.EventBusSubscriber; -@EventBusSubscriber(value = Dist.CLIENT) +@EventBusSubscriber public class ZapperInteractionHandler { - @SubscribeEvent - public static void leftClickingTheZapperSelectsANewBlock(PlayerInteractEvent.LeftClickEmpty event) { - ItemStack heldItem = event.getPlayer() - .getHeldItemMainhand(); - if (heldItem.getItem() instanceof ZapperItem && trySelect(heldItem, event.getPlayer())) - AllPackets.channel.sendToServer(new NbtPacket(heldItem, Hand.MAIN_HAND)); - } - @SubscribeEvent public static void leftClickingBlocksWithTheZapperSelectsTheBlock(PlayerInteractEvent.LeftClickBlock event) { - ItemStack heldItem = event.getPlayer() - .getHeldItemMainhand(); + if (event.getWorld().isRemote) + return; + ItemStack heldItem = event.getPlayer().getHeldItemMainhand(); if (heldItem.getItem() instanceof ZapperItem && trySelect(heldItem, event.getPlayer())) { event.setCancellationResult(ActionResultType.FAIL); event.setCanceled(true); - AllPackets.channel.sendToServer(new NbtPacket(heldItem, Hand.MAIN_HAND)); } } @@ -111,7 +99,7 @@ public class ZapperInteractionHandler { tag.remove("BlockData"); else tag.put("BlockData", data); - player.world.playSound(player, player.getPosition(), AllSoundEvents.BLOCKZAPPER_CONFIRM.get(), + player.world.playSound(null, player.getPosition(), AllSoundEvents.BLOCKZAPPER_CONFIRM.get(), SoundCategory.BLOCKS, 0.5f, 0.8f); return true; diff --git a/src/main/java/com/simibubi/create/events/ClientEvents.java b/src/main/java/com/simibubi/create/events/ClientEvents.java index 5f5e8c978..a609e7955 100644 --- a/src/main/java/com/simibubi/create/events/ClientEvents.java +++ b/src/main/java/com/simibubi/create/events/ClientEvents.java @@ -19,6 +19,7 @@ import com.simibubi.create.content.contraptions.components.structureMovement.tra import com.simibubi.create.content.contraptions.components.turntable.TurntableHandler; import com.simibubi.create.content.contraptions.relays.belt.item.BeltConnectorHandler; import com.simibubi.create.content.curiosities.tools.ExtendoGripRenderHandler; +import com.simibubi.create.content.curiosities.zapper.ZapperItem; import com.simibubi.create.content.curiosities.zapper.ZapperRenderHandler; import com.simibubi.create.content.curiosities.zapper.blockzapper.BlockzapperRenderHandler; import com.simibubi.create.content.curiosities.zapper.terrainzapper.WorldshaperRenderHandler; @@ -26,6 +27,8 @@ import com.simibubi.create.content.logistics.block.mechanicalArm.ArmInteractionP import com.simibubi.create.foundation.config.AllConfigs; import com.simibubi.create.foundation.gui.ScreenOpener; import com.simibubi.create.foundation.item.TooltipHelper; +import com.simibubi.create.foundation.networking.AllPackets; +import com.simibubi.create.foundation.networking.LeftClickPacket; import com.simibubi.create.foundation.renderState.SuperRenderTypeBuffer; import com.simibubi.create.foundation.tileEntity.behaviour.edgeInteraction.EdgeInteractionRenderer; import com.simibubi.create.foundation.tileEntity.behaviour.filtering.FilteringRenderer; @@ -53,6 +56,7 @@ import net.minecraftforge.event.TickEvent.ClientTickEvent; import net.minecraftforge.event.TickEvent.Phase; import net.minecraftforge.event.TickEvent.RenderTickEvent; import net.minecraftforge.event.entity.player.ItemTooltipEvent; +import net.minecraftforge.event.entity.player.PlayerInteractEvent; import net.minecraftforge.event.world.WorldEvent; import net.minecraftforge.eventbus.api.SubscribeEvent; import net.minecraftforge.fml.common.Mod.EventBusSubscriber; @@ -216,4 +220,12 @@ public class ClientEvents { } } + @SubscribeEvent + public static void leftClickEmpty(PlayerInteractEvent.LeftClickEmpty event) { + ItemStack stack = event.getItemStack(); + if (stack.getItem() instanceof ZapperItem) { + AllPackets.channel.sendToServer(new LeftClickPacket()); + } + } + } diff --git a/src/main/java/com/simibubi/create/events/CommonEvents.java b/src/main/java/com/simibubi/create/events/CommonEvents.java index e3dd7bf80..e8422f75f 100644 --- a/src/main/java/com/simibubi/create/events/CommonEvents.java +++ b/src/main/java/com/simibubi/create/events/CommonEvents.java @@ -8,6 +8,8 @@ import com.simibubi.create.content.contraptions.components.structureMovement.tra import com.simibubi.create.content.contraptions.fluids.recipe.FluidTransferRecipes; import com.simibubi.create.content.contraptions.fluids.recipe.PotionMixingRecipeManager; import com.simibubi.create.content.contraptions.wrench.WrenchItem; +import com.simibubi.create.content.curiosities.zapper.ZapperInteractionHandler; +import com.simibubi.create.content.curiosities.zapper.ZapperItem; import com.simibubi.create.content.schematics.ServerSchematicLoader; import com.simibubi.create.foundation.command.AllCommands; import com.simibubi.create.foundation.fluid.FluidHelper; @@ -19,7 +21,9 @@ import com.simibubi.create.foundation.utility.recipe.RecipeFinder; import net.minecraft.block.BlockState; import net.minecraft.entity.Entity; import net.minecraft.entity.LivingEntity; +import net.minecraft.entity.player.ServerPlayerEntity; import net.minecraft.fluid.IFluidState; +import net.minecraft.item.ItemStack; import net.minecraft.resources.IReloadableResourceManager; import net.minecraft.tags.FluidTags; import net.minecraft.util.Direction; @@ -159,4 +163,11 @@ public class CommonEvents { CapabilityMinecartController.startTracking(event); } + public static void leftClickEmpty(ServerPlayerEntity player) { + ItemStack stack = player.getHeldItemMainhand(); + if (stack.getItem() instanceof ZapperItem) { + ZapperInteractionHandler.trySelect(stack, player); + } + } + } diff --git a/src/main/java/com/simibubi/create/foundation/networking/AllPackets.java b/src/main/java/com/simibubi/create/foundation/networking/AllPackets.java index 0d4dbf5fa..94222fb36 100644 --- a/src/main/java/com/simibubi/create/foundation/networking/AllPackets.java +++ b/src/main/java/com/simibubi/create/foundation/networking/AllPackets.java @@ -64,6 +64,7 @@ public enum AllPackets { MINECART_COUPLING_CREATION(CouplingCreationPacket.class, CouplingCreationPacket::new), INSTANT_SCHEMATIC(InstantSchematicPacket.class, InstantSchematicPacket::new), SYNC_SCHEMATIC(SchematicSyncPacket.class, SchematicSyncPacket::new), + LEFT_CLICK(LeftClickPacket.class, LeftClickPacket::new), // Server to Client SYMMETRY_EFFECT(SymmetryEffectPacket.class, SymmetryEffectPacket::new), diff --git a/src/main/java/com/simibubi/create/foundation/networking/LeftClickPacket.java b/src/main/java/com/simibubi/create/foundation/networking/LeftClickPacket.java new file mode 100644 index 000000000..8ce69099b --- /dev/null +++ b/src/main/java/com/simibubi/create/foundation/networking/LeftClickPacket.java @@ -0,0 +1,32 @@ +package com.simibubi.create.foundation.networking; + +import java.util.function.Supplier; + +import com.simibubi.create.events.CommonEvents; + +import net.minecraft.network.PacketBuffer; +import net.minecraftforge.fml.network.NetworkDirection; +import net.minecraftforge.fml.network.NetworkEvent.Context; + +public class LeftClickPacket extends SimplePacketBase { + + public LeftClickPacket() { + } + + LeftClickPacket(PacketBuffer buffer) { + } + + @Override + public void write(PacketBuffer buffer) { + } + + @Override + public void handle(Supplier context) { + Context ctx = context.get(); + if (ctx.getDirection() != NetworkDirection.PLAY_TO_SERVER) + return; + ctx.enqueueWork(() -> CommonEvents.leftClickEmpty(ctx.getSender())); + ctx.setPacketHandled(true); + } + +}