Access Terraformer

This commit is contained in:
Dragon2488 2017-08-11 23:42:49 +07:00
parent bd89829b88
commit 6261b32e87
11 changed files with 160 additions and 2131 deletions

View file

@ -12,6 +12,7 @@ import net.minecraft.inventory.Container;
import net.minecraft.inventory.ISidedInventory; import net.minecraft.inventory.ISidedInventory;
import net.minecraft.item.ItemStack; import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound; import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.network.PacketBuffer;
import net.minecraft.util.EnumFacing; import net.minecraft.util.EnumFacing;
import net.minecraft.util.ResourceLocation; import net.minecraft.util.ResourceLocation;
import net.minecraft.util.math.BlockPos; import net.minecraft.util.math.BlockPos;
@ -64,11 +65,7 @@ public interface IMetaTileEntity extends ITurnable, IMachineBlockUpdateable, IRe
*/ */
void loadNBTData(NBTTagCompound data); void loadNBTData(NBTTagCompound data);
/** void receiveCustomData(int dataId, PacketBuffer buf);
* If a Cover of that Type can be placed on this Side.
* Also Called when the Facing of the Block Changes and a Cover is on said Side.
*/
boolean allowCoverOnSide(EnumFacing side, int coverId);
/** /**
* When a Player rightclicks the Facing with a Screwdriver. * When a Player rightclicks the Facing with a Screwdriver.

View file

@ -0,0 +1,9 @@
package gregtech.api.interfaces.tileentity;
import net.minecraft.network.PacketBuffer;
public interface ICustomDataTile {
void receiveCustomData(PacketBuffer buf);
}

View file

@ -13,7 +13,7 @@ import net.minecraft.util.EnumFacing;
* <p/> * <p/>
* This is all you need to connect to the GT Network. * This is all you need to connect to the GT Network.
* IColoredTileEntity is needed for not connecting differently coloured Blocks to each other. * IColoredTileEntity is needed for not connecting differently coloured Blocks to each other.
* IHasWorldObjectAndCoords is needed for the InWorld related Stuff. @BaseTileEntity does implement most of that Interface. * IHasWorldObjectAndCoords is needed for the InWorld related Stuff. @TickableTileEntityBase does implement most of that Interface.
*/ */
public interface IEnergyConnected extends IColoredTileEntity, IHasWorldObjectAndCoords, IEnergyAcceptor, IEnergyEmitter { public interface IEnergyConnected extends IColoredTileEntity, IHasWorldObjectAndCoords, IEnergyAcceptor, IEnergyEmitter {

View file

@ -4,12 +4,19 @@ import com.google.common.base.Preconditions;
import gregtech.api.GregTech_API; import gregtech.api.GregTech_API;
import gregtech.api.interfaces.metatileentity.IMetaTileEntity; import gregtech.api.interfaces.metatileentity.IMetaTileEntity;
import gregtech.api.interfaces.metatileentity.IMetaTileEntityFactory; import gregtech.api.interfaces.metatileentity.IMetaTileEntityFactory;
import gregtech.api.interfaces.tileentity.ICustomDataTile;
import gregtech.api.interfaces.tileentity.IGregTechTileEntity; import gregtech.api.interfaces.tileentity.IGregTechTileEntity;
import gregtech.api.net.NetworkHandler;
import gregtech.api.net.PacketCustomTileData;
import io.netty.buffer.Unpooled;
import net.minecraft.entity.player.EntityPlayer; import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.ISidedInventory; import net.minecraft.inventory.ISidedInventory;
import net.minecraft.item.ItemStack; import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound; import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.network.PacketBuffer;
import net.minecraft.server.management.PlayerChunkMapEntry;
import net.minecraft.util.EnumFacing; import net.minecraft.util.EnumFacing;
import net.minecraft.world.WorldServer;
import net.minecraftforge.common.capabilities.Capability; import net.minecraftforge.common.capabilities.Capability;
import net.minecraftforge.common.util.Constants; import net.minecraftforge.common.util.Constants;
import net.minecraftforge.fluids.Fluid; import net.minecraftforge.fluids.Fluid;
@ -18,12 +25,14 @@ import net.minecraftforge.fluids.FluidTankInfo;
import net.minecraftforge.fluids.IFluidHandler; import net.minecraftforge.fluids.IFluidHandler;
import net.minecraftforge.fluids.capability.CapabilityFluidHandler; import net.minecraftforge.fluids.capability.CapabilityFluidHandler;
import net.minecraftforge.fluids.capability.wrappers.FluidHandlerWrapper; import net.minecraftforge.fluids.capability.wrappers.FluidHandlerWrapper;
import net.minecraftforge.fml.common.network.NetworkRegistry;
import net.minecraftforge.items.CapabilityItemHandler; import net.minecraftforge.items.CapabilityItemHandler;
import net.minecraftforge.items.wrapper.SidedInvWrapper; import net.minecraftforge.items.wrapper.SidedInvWrapper;
import javax.annotation.Nullable; import javax.annotation.Nullable;
import java.util.function.Consumer;
public class GregtechTileEntity extends BaseTileEntity implements IGregTechTileEntity, ISidedInventory, IFluidHandler { public class GregtechTileEntity extends TickableTileEntityBase implements IGregTechTileEntity, ICustomDataTile, ISidedInventory, IFluidHandler {
private MetaTileEntity metaTileEntity; private MetaTileEntity metaTileEntity;
@ -38,8 +47,11 @@ public class GregtechTileEntity extends BaseTileEntity implements IGregTechTileE
Preconditions.checkArgument(metaTileEntity instanceof MetaTileEntity, "GregtechTileEntity supports only MetaTileEntity child!"); Preconditions.checkArgument(metaTileEntity instanceof MetaTileEntity, "GregtechTileEntity supports only MetaTileEntity child!");
this.metaTileEntity = (MetaTileEntity) metaTileEntity; this.metaTileEntity = (MetaTileEntity) metaTileEntity;
this.metaTileEntity.holder = this; this.metaTileEntity.holder = this;
if(!worldObj.isRemote) {
markDirty(); markDirty();
} }
}
@Override @Override
public void readFromNBT(NBTTagCompound compound) { public void readFromNBT(NBTTagCompound compound) {
@ -65,8 +77,25 @@ public class GregtechTileEntity extends BaseTileEntity implements IGregTechTileE
} }
@Override @Override
public boolean receiveClientEvent(int id, int type) { public void receiveCustomData(PacketBuffer buf) {
return super.receiveClientEvent(id, type); int dataId = buf.readInt();
switch (dataId) {
case 0:
IMetaTileEntityFactory factory = GregTech_API.METATILEENTITY_REGISTRY.getObjectById(buf.readShort());
this.metaTileEntity = factory.constructMetaTileEntity();
}
}
public void writeCustomData(int dataId, Consumer<PacketBuffer> dataWriter) {
PacketBuffer packetBuffer = new PacketBuffer(Unpooled.buffer());
packetBuffer.writeInt(dataId);
dataWriter.accept(packetBuffer);
WorldServer worldServer = (WorldServer) worldObj;
PlayerChunkMapEntry entry = worldServer.getPlayerChunkMap().getEntry(pos.getX() >> 4, pos.getZ() >> 4);
if(entry != null) {
}
} }
@Override @Override

View file

@ -1,17 +1,12 @@
package gregtech.api.metatileentity; package gregtech.api.metatileentity;
import gregtech.api.interfaces.tileentity.IHasWorldObjectAndCoords; import gregtech.api.interfaces.tileentity.IHasWorldObjectAndCoords;
import gregtech.api.net.GT_Packet_Block_Event;
import gregtech.api.util.GT_Utility;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.tileentity.TileEntity; import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.ITickable; import net.minecraft.util.ITickable;
import net.minecraft.util.math.BlockPos; import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World; import net.minecraft.world.World;
import static gregtech.api.GT_Values.NW; public abstract class TickableTileEntityBase extends TileEntity implements IHasWorldObjectAndCoords, ITickable {
public abstract class BaseTileEntity extends TileEntity implements IHasWorldObjectAndCoords, ITickable {
private long timer = 0L; private long timer = 0L;

View file

@ -1,17 +1,23 @@
package gregtech.api.net; package gregtech.api.net;
import com.google.common.collect.BiMap;
import com.google.common.collect.HashBiMap;
import gregtech.api.GT_Values; import gregtech.api.GT_Values;
import io.netty.buffer.ByteBuf; import gregtech.api.interfaces.tileentity.ICustomDataTile;
import io.netty.buffer.Unpooled; import io.netty.buffer.Unpooled;
import net.minecraft.client.Minecraft;
import net.minecraft.client.network.NetHandlerPlayClient;
import net.minecraft.network.INetHandler;
import net.minecraft.network.NetHandlerPlayServer;
import net.minecraft.network.PacketBuffer; import net.minecraft.network.PacketBuffer;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.IntIdentityHashBiMap; import net.minecraft.util.IntIdentityHashBiMap;
import net.minecraftforge.fml.common.FMLCommonHandler;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent; import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
import net.minecraftforge.fml.common.network.FMLEventChannel; import net.minecraftforge.fml.common.network.FMLEventChannel;
import net.minecraftforge.fml.common.network.FMLNetworkEvent; import net.minecraftforge.fml.common.network.FMLNetworkEvent;
import net.minecraftforge.fml.common.network.NetworkRegistry; import net.minecraftforge.fml.common.network.NetworkRegistry;
import net.minecraftforge.fml.common.network.internal.FMLProxyPacket; import net.minecraftforge.fml.common.network.internal.FMLProxyPacket;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
import java.util.HashMap; import java.util.HashMap;
import java.util.function.Consumer; import java.util.function.Consumer;
@ -30,6 +36,11 @@ public class NetworkHandler {
T decode(PacketBuffer byteBuf); T decode(PacketBuffer byteBuf);
} }
@FunctionalInterface
public interface PacketExecutor<T, R extends INetHandler> {
void execute(T packet, R handler);
}
public static final class PacketCodec<T extends Packet> { public static final class PacketCodec<T extends Packet> {
public final PacketEncoder<T> encoder; public final PacketEncoder<T> encoder;
@ -41,10 +52,10 @@ public class NetworkHandler {
} }
} }
public static final HashMap<Class<? extends Packet>, PacketCodec<? extends Packet>> codecMap = new HashMap<>(); private static final HashMap<Class<? extends Packet>, PacketCodec<? extends Packet>> codecMap = new HashMap<>();
public static final HashMap<Class<? extends Packet>, Consumer<? extends Packet>> clientExecutors = new HashMap<>(); @SideOnly(Side.CLIENT) private static final HashMap<Class<? extends Packet>, PacketExecutor<? extends Packet, NetHandlerPlayClient>> clientExecutors = new HashMap<>();
public static final HashMap<Class<? extends Packet>, Consumer<? extends Packet>> serverExecutors = new HashMap<>(); private static final HashMap<Class<? extends Packet>, PacketExecutor<? extends Packet, NetHandlerPlayServer>> serverExecutors = new HashMap<>();
public static final IntIdentityHashBiMap<Class<? extends Packet>> packetMap = new IntIdentityHashBiMap<>(10); private static final IntIdentityHashBiMap<Class<? extends Packet>> packetMap = new IntIdentityHashBiMap<>(10);
public static FMLEventChannel channel; public static FMLEventChannel channel;
@ -53,6 +64,47 @@ public class NetworkHandler {
public static void init() { public static void init() {
channel = NetworkRegistry.INSTANCE.newEventDrivenChannel(GT_Values.MODID); channel = NetworkRegistry.INSTANCE.newEventDrivenChannel(GT_Values.MODID);
channel.register(new NetworkHandler()); channel.register(new NetworkHandler());
registerPacket(0, PacketCustomTileData.class, new PacketCodec<>(
(packet, buf) -> {
buf.writeBlockPos(packet.tileEntityPos);
byte[] data = packet.payload.array();
buf.writeInt(data.length);
buf.writeBytes(data);
},
(buf) -> new PacketCustomTileData(
buf.readBlockPos(),
new PacketBuffer(buf.readBytes(buf.readInt()))
)
));
if(FMLCommonHandler.instance().getSide().isClient()) {
initClient();
}
}
@SideOnly(Side.CLIENT)
private static void initClient() {
registerClientExecutor(PacketCustomTileData.class, (packet, handler) -> {
TileEntity tileEntity = Minecraft.getMinecraft().theWorld.getTileEntity(packet.tileEntityPos);
if(tileEntity instanceof ICustomDataTile) {
((ICustomDataTile) tileEntity).receiveCustomData(packet.payload);
}
});
}
public static <T extends Packet> void registerPacket(int packetId, Class<T> packetClass, PacketCodec<T> codec) {
packetMap.put(packetClass, packetId);
codecMap.put(packetClass, codec);
}
@SideOnly(Side.CLIENT)
public static <T extends Packet> void registerClientExecutor(Class<T> packet, PacketExecutor<T, NetHandlerPlayClient> executor) {
clientExecutors.put(packet, executor);
}
public static <T extends Packet> void registerServerExecutor(Class<T> packet, PacketExecutor<T, NetHandlerPlayServer> executor) {
serverExecutors.put(packet, executor);
} }
public static FMLProxyPacket packet2proxy(Packet packet) { public static FMLProxyPacket packet2proxy(Packet packet) {
@ -71,11 +123,12 @@ public class NetworkHandler {
} }
@SubscribeEvent @SubscribeEvent
@SideOnly(Side.CLIENT)
public void onClientPacket(FMLNetworkEvent.ClientCustomPacketEvent event) { public void onClientPacket(FMLNetworkEvent.ClientCustomPacketEvent event) {
Packet packet = proxy2packet(event.getPacket()); Packet packet = proxy2packet(event.getPacket());
if(clientExecutors.containsKey(packet.getClass())) { if(clientExecutors.containsKey(packet.getClass())) {
Consumer<Packet> c = (Consumer<Packet>) clientExecutors.get(packet.getClass()); PacketExecutor<Packet, NetHandlerPlayClient> executor = (PacketExecutor<Packet, NetHandlerPlayClient>) clientExecutors.get(packet.getClass());
c.accept(packet); executor.execute(packet, (NetHandlerPlayClient) event.getHandler());
} }
} }
@ -83,8 +136,8 @@ public class NetworkHandler {
public void onServerPacket(FMLNetworkEvent.ServerCustomPacketEvent event) { public void onServerPacket(FMLNetworkEvent.ServerCustomPacketEvent event) {
Packet packet = proxy2packet(event.getPacket()); Packet packet = proxy2packet(event.getPacket());
if(serverExecutors.containsKey(packet.getClass())) { if(serverExecutors.containsKey(packet.getClass())) {
Consumer<Packet> c = (Consumer<Packet>) serverExecutors.get(packet.getClass()); PacketExecutor<Packet, NetHandlerPlayServer> executor = (PacketExecutor<Packet, NetHandlerPlayServer>) serverExecutors.get(packet.getClass());
c.accept(packet); executor.execute(packet, (NetHandlerPlayServer) event.getHandler());
} }
} }

View file

@ -0,0 +1,42 @@
package gregtech.api.net;
import net.minecraft.network.PacketBuffer;
import net.minecraft.util.math.BlockPos;
public class PacketCustomTileData implements NetworkHandler.Packet {
public final BlockPos tileEntityPos;
public final PacketBuffer payload;
public PacketCustomTileData(BlockPos tileEntityPos, PacketBuffer payload) {
this.tileEntityPos = tileEntityPos;
this.payload = payload;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
PacketCustomTileData that = (PacketCustomTileData) o;
if (!tileEntityPos.equals(that.tileEntityPos)) return false;
return payload.equals(that.payload);
}
@Override
public int hashCode() {
int result = tileEntityPos.hashCode();
result = 31 * result + payload.hashCode();
return result;
}
@Override
public String toString() {
return "PacketCustomTileData{" +
"tileEntityPos=" + tileEntityPos +
", payload=" + payload.hashCode() +
'}';
}
}

View file

@ -10,7 +10,7 @@ import gregtech.api.interfaces.tileentity.IGregTechTileEntity;
import gregtech.api.items.GenericBlock; import gregtech.api.items.GenericBlock;
import gregtech.api.metatileentity.BaseMetaPipeEntity; import gregtech.api.metatileentity.BaseMetaPipeEntity;
import gregtech.api.metatileentity.BaseMetaTileEntity; import gregtech.api.metatileentity.BaseMetaTileEntity;
import gregtech.api.metatileentity.BaseTileEntity; import gregtech.api.metatileentity.TickableTileEntityBase;
import gregtech.api.metatileentity.implementations.GT_MetaTileEntity_TieredMachineBlock; import gregtech.api.metatileentity.implementations.GT_MetaTileEntity_TieredMachineBlock;
import gregtech.api.objects.GT_RenderedTexture; import gregtech.api.objects.GT_RenderedTexture;
import gregtech.api.util.GT_Log; import gregtech.api.util.GT_Log;
@ -91,8 +91,8 @@ public class BlockMachines extends GenericBlock implements IDebugableBlock, ITil
@Override @Override
public void onNeighborChange(IBlockAccess world, BlockPos pos, BlockPos neighbor) { public void onNeighborChange(IBlockAccess world, BlockPos pos, BlockPos neighbor) {
TileEntity tileEntity = world.getTileEntity(pos); TileEntity tileEntity = world.getTileEntity(pos);
if ((tileEntity instanceof BaseTileEntity)) { if ((tileEntity instanceof TickableTileEntityBase)) {
((BaseTileEntity) tileEntity).onAdjacentBlockChange(neighbor.getX(), neighbor.getY(), neighbor.getZ()); ((TickableTileEntityBase) tileEntity).onAdjacentBlockChange(neighbor.getX(), neighbor.getY(), neighbor.getZ());
} }
} }

View file

@ -6,7 +6,7 @@ import gregtech.api.gui.GT_GUIContainer_BasicTank;
import gregtech.api.interfaces.ITexture; import gregtech.api.interfaces.ITexture;
import gregtech.api.interfaces.metatileentity.IMetaTileEntity; import gregtech.api.interfaces.metatileentity.IMetaTileEntity;
import gregtech.api.interfaces.tileentity.IGregTechTileEntity; import gregtech.api.interfaces.tileentity.IGregTechTileEntity;
import gregtech.api.metatileentity.BaseTileEntity; import gregtech.api.metatileentity.TickableTileEntityBase;
import gregtech.api.metatileentity.implementations.GT_MetaTileEntity_Hatch; import gregtech.api.metatileentity.implementations.GT_MetaTileEntity_Hatch;
import gregtech.api.objects.GT_RenderedTexture; import gregtech.api.objects.GT_RenderedTexture;
import gregtech.api.util.GT_ModHandler; import gregtech.api.util.GT_ModHandler;
@ -115,8 +115,8 @@ public class GT_MetaTileEntity_Pump extends GT_MetaTileEntity_Hatch {
super.onPostTick(aBaseMetaTileEntity, aTick); super.onPostTick(aBaseMetaTileEntity, aTick);
if (getBaseMetaTileEntity().isServerSide()) { if (getBaseMetaTileEntity().isServerSide()) {
this.mPumpTimer -= 1; this.mPumpTimer -= 1;
if ((getBaseMetaTileEntity() instanceof BaseTileEntity)) { if ((getBaseMetaTileEntity() instanceof TickableTileEntityBase)) {
((BaseTileEntity) getBaseMetaTileEntity()).ignoreUnloadedChunks = false; ((TickableTileEntityBase) getBaseMetaTileEntity()).ignoreUnloadedChunks = false;
} }
this.doTickProfilingInThisTick = true; this.doTickProfilingInThisTick = true;
this.mPumpCountBelow = 0; this.mPumpCountBelow = 0;

View file

@ -0,0 +1,2 @@
#PlayerChunkMapEntry
public net.minecraft.server.management.PlayerChunkMapEntry field_187283_c #players