Skip to content

Commit 0ec33f0

Browse files
committed
move logic into hook class
1 parent 17691d5 commit 0ec33f0

File tree

2 files changed

+76
-39
lines changed

2 files changed

+76
-39
lines changed
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package gregtech.client.utils;
2+
3+
import gregtech.api.GregTechAPI;
4+
import gregtech.api.metatileentity.MetaTileEntity;
5+
import gregtech.api.metatileentity.registry.MTERegistry;
6+
7+
import net.minecraft.client.network.NetHandlerPlayClient;
8+
import net.minecraft.nbt.NBTTagCompound;
9+
import net.minecraft.network.PacketBuffer;
10+
import net.minecraft.network.play.server.SPacketChunkData;
11+
import net.minecraft.util.ResourceLocation;
12+
import net.minecraft.util.math.BlockPos;
13+
import net.minecraft.world.World;
14+
import net.minecraft.world.chunk.Chunk;
15+
16+
import org.spongepowered.asm.mixin.Unique;
17+
18+
import java.util.List;
19+
20+
public class ClientHandlerHooks {
21+
22+
/**
23+
* Initializes any MetaTileEntities right before {@link Chunk#read(PacketBuffer, int, boolean) Chunk.read()} is
24+
* called in
25+
* {@link NetHandlerPlayClient#handleChunkData(SPacketChunkData) handleChunkData()}.
26+
*
27+
* @param packetIn Chunk Data Packet
28+
*/
29+
public static void handleTags(World world, List<NBTTagCompound> tagCompounds) {
30+
tagCompounds.forEach(tagCompound -> handleTag(world, tagCompound));
31+
}
32+
33+
/**
34+
* Initializes the MetaTileEntity before it handles
35+
*
36+
* @param packetIn Tile Entity Update Packet
37+
*/
38+
public static void handleTag(World world, NBTTagCompound tagCompound) {
39+
MetaTileEntity mte = fromTag(tagCompound);
40+
if (mte != null) {
41+
BlockPos pos = new BlockPos(tagCompound.getInteger("x"), tagCompound.getInteger("y"),
42+
tagCompound.getInteger("z"));
43+
placeTile(world, mte, pos);
44+
}
45+
}
46+
47+
/**
48+
* Creates the correct MetaTileEntity from the given tag and sets the tile entity to the world
49+
* IF a TileEntity doesn't already exist at the position
50+
*
51+
* @param tag TileEntity tag data
52+
*/
53+
@Unique
54+
private static MetaTileEntity fromTag(NBTTagCompound tag) {
55+
if (tag.hasKey("MetaId")) {
56+
ResourceLocation metaId = new ResourceLocation(tag.getString("MetaId"));
57+
MTERegistry registry = GregTechAPI.mteManager.getRegistry(metaId.getNamespace());
58+
return registry.getObject(metaId);
59+
}
60+
return null;
61+
}
62+
63+
private static void placeTile(World world, MetaTileEntity mte, BlockPos pos) {
64+
// set te in world directly
65+
// check if world contains a TE at this pos?
66+
// is null checking good enough?
67+
if (world.getChunk(pos).getTileEntity(pos, Chunk.EnumCreateEntityType.CHECK) == null) {
68+
if (world.getBlockState(pos).getBlock() != mte.getBlock())
69+
world.setBlockState(pos, mte.getBlock().getDefaultState());
70+
world.setTileEntity(pos, mte.copy());
71+
}
72+
}
73+
}
Lines changed: 3 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,16 @@
11
package gregtech.mixins.minecraft;
22

3-
import gregtech.api.GregTechAPI;
4-
import gregtech.api.metatileentity.MetaTileEntity;
5-
import gregtech.api.metatileentity.registry.MTERegistry;
3+
import gregtech.client.utils.ClientHandlerHooks;
64

75
import net.minecraft.client.multiplayer.WorldClient;
86
import net.minecraft.client.network.NetHandlerPlayClient;
9-
import net.minecraft.nbt.NBTTagCompound;
107
import net.minecraft.network.PacketBuffer;
118
import net.minecraft.network.play.server.SPacketChunkData;
129
import net.minecraft.network.play.server.SPacketUpdateTileEntity;
13-
import net.minecraft.util.ResourceLocation;
14-
import net.minecraft.util.math.BlockPos;
15-
import net.minecraft.world.World;
1610
import net.minecraft.world.chunk.Chunk;
1711

1812
import org.spongepowered.asm.mixin.Mixin;
1913
import org.spongepowered.asm.mixin.Shadow;
20-
import org.spongepowered.asm.mixin.Unique;
2114
import org.spongepowered.asm.mixin.injection.At;
2215
import org.spongepowered.asm.mixin.injection.Inject;
2316
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
@@ -40,9 +33,7 @@ public abstract class ClientHandlerMixin {
4033
target = "Lnet/minecraft/world/chunk/Chunk;read(Lnet/minecraft/network/PacketBuffer;IZ)V"))
4134
public void initClientTiles(SPacketChunkData packetIn, CallbackInfo ci) {
4235
// todo this might not be necessary anymore, though the TE does need to be initialized for initial sync data
43-
for (NBTTagCompound tag : packetIn.getTileEntityTags()) {
44-
gregTech$initMetaTile(this.world, tag);
45-
}
36+
ClientHandlerHooks.handleTags(this.world, packetIn.getTileEntityTags());
4637
}
4738

4839
/**
@@ -54,33 +45,6 @@ public void initClientTiles(SPacketChunkData packetIn, CallbackInfo ci) {
5445
at = @At(value = "INVOKE",
5546
target = "Lnet/minecraft/client/multiplayer/WorldClient;getTileEntity(Lnet/minecraft/util/math/BlockPos;)Lnet/minecraft/tileentity/TileEntity;"))
5647
public void initClientTile(SPacketUpdateTileEntity packetIn, CallbackInfo ci) {
57-
gregTech$initMetaTile(this.world, packetIn.getNbtCompound());
58-
}
59-
60-
// todo move this into a hook class or something
61-
/**
62-
* Creates the correct MetaTileEntity from the given tag and sets the tile entity to the world
63-
* IF a TileEntity doesn't already exist at the position
64-
*
65-
* @param tag TileEntity tag data
66-
*/
67-
@Unique
68-
public void gregTech$initMetaTile(World world, NBTTagCompound tag) {
69-
if (!tag.hasKey("MetaId")) return;
70-
71-
ResourceLocation metaId = new ResourceLocation(tag.getString("MetaId"));
72-
MTERegistry registry = GregTechAPI.mteManager.getRegistry(metaId.getNamespace());
73-
MetaTileEntity mte = registry.getObject(metaId);
74-
BlockPos pos = new BlockPos(tag.getInteger("x"), tag.getInteger("y"), tag.getInteger("z"));
75-
if (mte == null) return;
76-
77-
// set te in world directly
78-
// check if world contains a TE at this pos?
79-
// is null checking good enough?
80-
if (world.getChunk(pos).getTileEntity(pos, Chunk.EnumCreateEntityType.CHECK) == null) {
81-
if (world.getBlockState(pos).getBlock() != registry.getBlock())
82-
world.setBlockState(pos, registry.getBlock().getDefaultState());
83-
world.setTileEntity(pos, mte.copy());
84-
}
48+
ClientHandlerHooks.handleTag(this.world, packetIn.getNbtCompound());
8549
}
8650
}

0 commit comments

Comments
 (0)