Packet workaround

This commit is contained in:
Snownee 2020-11-10 00:51:56 +08:00
parent 6b34eb453d
commit 196083cb00
4 changed files with 89 additions and 13 deletions

View file

@ -12,9 +12,9 @@ import com.simibubi.create.content.schematics.SchematicWorld;
import com.simibubi.create.content.schematics.client.tools.Tools; import com.simibubi.create.content.schematics.client.tools.Tools;
import com.simibubi.create.content.schematics.item.SchematicItem; import com.simibubi.create.content.schematics.item.SchematicItem;
import com.simibubi.create.content.schematics.packet.SchematicPlacePacket; import com.simibubi.create.content.schematics.packet.SchematicPlacePacket;
import com.simibubi.create.content.schematics.packet.SchematicSyncPacket;
import com.simibubi.create.foundation.gui.ToolSelectionScreen; import com.simibubi.create.foundation.gui.ToolSelectionScreen;
import com.simibubi.create.foundation.networking.AllPackets; import com.simibubi.create.foundation.networking.AllPackets;
import com.simibubi.create.foundation.networking.NbtPacket;
import com.simibubi.create.foundation.renderState.SuperRenderTypeBuffer; import com.simibubi.create.foundation.renderState.SuperRenderTypeBuffer;
import com.simibubi.create.foundation.utility.outliner.AABBOutline; import com.simibubi.create.foundation.utility.outliner.AABBOutline;
@ -272,17 +272,7 @@ public class SchematicHandler {
public void sync() { public void sync() {
if (activeSchematicItem == null) if (activeSchematicItem == null)
return; return;
AllPackets.channel.sendToServer(new SchematicSyncPacket(activeHotbarSlot, transformation.toSettings(), transformation.getAnchor(), deployed));
PlacementSettings settings = transformation.toSettings();
CompoundNBT tag = activeSchematicItem.getTag();
tag.putBoolean("Deployed", deployed);
tag.put("Anchor", NBTUtil.writeBlockPos(transformation.getAnchor()));
tag.putString("Rotation", settings.getRotation()
.name());
tag.putString("Mirror", settings.getMirror()
.name());
AllPackets.channel.sendToServer(new NbtPacket(activeSchematicItem, activeHotbarSlot));
} }
public void equip(Tools tool) { public void equip(Tools tool) {

View file

@ -0,0 +1,77 @@
package com.simibubi.create.content.schematics.packet;
import java.util.function.Supplier;
import com.simibubi.create.AllItems;
import com.simibubi.create.foundation.networking.SimplePacketBase;
import net.minecraft.entity.player.ServerPlayerEntity;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.CompoundNBT;
import net.minecraft.nbt.NBTUtil;
import net.minecraft.network.PacketBuffer;
import net.minecraft.util.Mirror;
import net.minecraft.util.Rotation;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.gen.feature.template.PlacementSettings;
import net.minecraftforge.fml.network.NetworkEvent.Context;
public class SchematicSyncPacket extends SimplePacketBase {
public int slot;
public boolean deployed;
public BlockPos anchor;
public Rotation rotation;
public Mirror mirror;
public SchematicSyncPacket(int slot, PlacementSettings settings,
BlockPos anchor, boolean deployed) {
this.slot = slot;
this.deployed = deployed;
this.anchor = anchor;
this.rotation = settings.getRotation();
this.mirror = settings.getMirror();
}
public SchematicSyncPacket(PacketBuffer buffer) {
slot = buffer.readVarInt();
deployed = buffer.readBoolean();
anchor = buffer.readBlockPos();
rotation = buffer.readEnumValue(Rotation.class);
mirror = buffer.readEnumValue(Mirror.class);
}
@Override
public void write(PacketBuffer buffer) {
buffer.writeVarInt(slot);
buffer.writeBoolean(deployed);
buffer.writeBlockPos(anchor);
buffer.writeEnumValue(rotation);
buffer.writeEnumValue(mirror);
}
@Override
public void handle(Supplier<Context> context) {
context.get().enqueueWork(() -> {
ServerPlayerEntity player = context.get().getSender();
if (player == null)
return;
ItemStack stack = ItemStack.EMPTY;
if (slot == -1) {
stack = player.getHeldItemMainhand();
} else {
stack = player.inventory.getStackInSlot(slot);
}
if (!AllItems.SCHEMATIC.isIn(stack)) {
return;
}
CompoundNBT tag = stack.getOrCreateTag();
tag.putBoolean("Deployed", deployed);
tag.put("Anchor", NBTUtil.writeBlockPos(anchor));
tag.putString("Rotation", rotation.name());
tag.putString("Mirror", mirror.name());
});
context.get().setPacketHandled(true);
}
}

View file

@ -26,6 +26,7 @@ import com.simibubi.create.content.schematics.packet.ConfigureSchematicannonPack
import com.simibubi.create.content.schematics.packet.InstantSchematicPacket; import com.simibubi.create.content.schematics.packet.InstantSchematicPacket;
import com.simibubi.create.content.schematics.packet.SchematicPlacePacket; import com.simibubi.create.content.schematics.packet.SchematicPlacePacket;
import com.simibubi.create.content.schematics.packet.SchematicUploadPacket; import com.simibubi.create.content.schematics.packet.SchematicUploadPacket;
import com.simibubi.create.content.schematics.packet.SchematicSyncPacket;
import com.simibubi.create.foundation.command.ConfigureConfigPacket; import com.simibubi.create.foundation.command.ConfigureConfigPacket;
import com.simibubi.create.foundation.tileEntity.behaviour.filtering.FilteringCountUpdatePacket; import com.simibubi.create.foundation.tileEntity.behaviour.filtering.FilteringCountUpdatePacket;
import com.simibubi.create.foundation.tileEntity.behaviour.scrollvalue.ScrollValueUpdatePacket; import com.simibubi.create.foundation.tileEntity.behaviour.scrollvalue.ScrollValueUpdatePacket;
@ -60,6 +61,7 @@ public enum AllPackets {
PLACE_ARM(ArmPlacementPacket.class, ArmPlacementPacket::new), PLACE_ARM(ArmPlacementPacket.class, ArmPlacementPacket::new),
MINECART_COUPLING_CREATION(CouplingCreationPacket.class, CouplingCreationPacket::new), MINECART_COUPLING_CREATION(CouplingCreationPacket.class, CouplingCreationPacket::new),
INSTANT_SCHEMATIC(InstantSchematicPacket.class, InstantSchematicPacket::new), INSTANT_SCHEMATIC(InstantSchematicPacket.class, InstantSchematicPacket::new),
SYNC_SCHEMATIC(SchematicSyncPacket.class, SchematicSyncPacket::new),
// Server to Client // Server to Client
SYMMETRY_EFFECT(SymmetryEffectPacket.class, SymmetryEffectPacket::new), SYMMETRY_EFFECT(SymmetryEffectPacket.class, SymmetryEffectPacket::new),

View file

@ -2,12 +2,16 @@ package com.simibubi.create.foundation.networking;
import java.util.function.Supplier; import java.util.function.Supplier;
import com.simibubi.create.content.curiosities.symmetry.SymmetryWandItem;
import com.simibubi.create.content.curiosities.zapper.ZapperItem;
import net.minecraft.entity.player.ServerPlayerEntity; import net.minecraft.entity.player.ServerPlayerEntity;
import net.minecraft.item.ItemStack; import net.minecraft.item.ItemStack;
import net.minecraft.network.PacketBuffer; import net.minecraft.network.PacketBuffer;
import net.minecraft.util.Hand; import net.minecraft.util.Hand;
import net.minecraftforge.fml.network.NetworkEvent.Context; import net.minecraftforge.fml.network.NetworkEvent.Context;
@Deprecated
public class NbtPacket extends SimplePacketBase { public class NbtPacket extends SimplePacketBase {
public ItemStack stack; public ItemStack stack;
@ -42,7 +46,10 @@ public class NbtPacket extends SimplePacketBase {
ServerPlayerEntity player = context.get().getSender(); ServerPlayerEntity player = context.get().getSender();
if (player == null) if (player == null)
return; return;
if (!(stack.getItem() instanceof SymmetryWandItem || stack.getItem() instanceof ZapperItem)) {
return;
}
stack.removeChildTag("AttributeModifiers");
if (slot == -1) { if (slot == -1) {
ItemStack heldItem = player.getHeldItem(hand); ItemStack heldItem = player.getHeldItem(hand);
if (heldItem.getItem() == stack.getItem()) { if (heldItem.getItem() == stack.getItem()) {