Make the zapper selection logic server-side (#515)

This commit is contained in:
Snownee 2020-12-12 19:32:32 +08:00
parent f64dc8c43f
commit 24dcb5383a
5 changed files with 61 additions and 17 deletions

View File

@ -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;

View File

@ -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());
}
}
}

View File

@ -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);
}
}
}

View File

@ -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),

View File

@ -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) {
Context ctx = context.get();
if (ctx.getDirection() != NetworkDirection.PLAY_TO_SERVER)
return;
ctx.enqueueWork(() -> CommonEvents.leftClickEmpty(ctx.getSender()));
ctx.setPacketHandled(true);
}
}