Add base block/TE classes, and some network stuff

This commit is contained in:
tterrag1098 committed 2015-05-03 23:26:10 -04:00
1 parent 2ffd22c2c1
commit 8beb043ff3
20 files changed
+829 -164

No files matched your search

@@ -6,10 +6,10 @@ import com.enderio.core.common.vecmath.Vertex;
public interface VertexTransform {
public abstract void apply(Vertex vertex);
void apply(Vertex vertex);
public abstract void apply(Vector3d vec);
void apply(Vector3d vec);
public abstract void applyToNormal(Vector3f vec);
void applyToNormal(Vector3f vec);
}
@@ -1,4 +1,4 @@
package com.enderio.core.common.util;
package com.enderio.core.api.common.util;
import net.minecraftforge.fluids.IFluidHandler;
@@ -1,4 +1,4 @@
package com.enderio.core.common.util;
package com.enderio.core.api.common.util;
import net.minecraft.item.ItemStack;
import net.minecraftforge.common.util.ForgeDirection;
@@ -1,4 +1,4 @@
package com.enderio.core.common.util;
package com.enderio.core.api.common.util;
import net.minecraft.tileentity.TileEntity;
import cpw.mods.fml.relauncher.Side;
@@ -1,4 +1,4 @@
package com.enderio.core.common.util;
package com.enderio.core.api.common.util;
import net.minecraftforge.fluids.FluidStack;
import net.minecraftforge.fluids.FluidTank;
@@ -1,15 +1,21 @@
package com.enderio.core.client;
import net.minecraft.client.Minecraft;
import net.minecraft.world.World;
import com.enderio.core.common.CommonProxy;
import com.enderio.core.common.util.Scheduler;
public class ClientProxy extends CommonProxy
{
private static final Scheduler scheduler = new Scheduler();
@Override
public Scheduler getScheduler()
{
return scheduler;
}
public class ClientProxy extends CommonProxy {
private static final Scheduler scheduler = new Scheduler();
@Override
public Scheduler getScheduler() {
return scheduler;
}
@Override
public World getClientWorld() {
return Minecraft.getMinecraft().theWorld;
}
}
@@ -0,0 +1,169 @@
package com.enderio.core.common;
import java.util.ArrayList;
import javax.annotation.Nullable;
import net.minecraft.block.Block;
import net.minecraft.block.material.MapColor;
import net.minecraft.block.material.Material;
import net.minecraft.client.renderer.texture.IIconRegister;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.world.World;
import net.minecraftforge.common.util.ForgeDirection;
import com.enderio.core.api.common.util.ITankAccess;
import com.enderio.core.common.util.FluidUtil;
import com.enderio.core.common.util.Log;
import com.google.common.collect.Lists;
import cpw.mods.fml.common.registry.GameRegistry;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
public abstract class BlockEnder extends Block {
protected final Class<? extends TileEntityEnder> teClass;
protected final String name;
protected BlockEnder(String name, Class<? extends TileEntityEnder> teClass) {
this(name, teClass, new Material(MapColor.ironColor));
}
protected BlockEnder(String name, Class<? extends TileEntityEnder> teClass, Material mat) {
super(mat);
this.teClass = teClass;
this.name = name;
setHardness(0.5F);
setBlockName(name);
setStepSound(Block.soundTypeMetal);
setHarvestLevel("pickaxe", 0);
}
protected void init() {
GameRegistry.registerBlock(this, name);
if (teClass != null) {
GameRegistry.registerTileEntity(teClass, name + "TileEntity");
}
}
@Override
public boolean hasTileEntity(int metadata) {
return teClass != null;
}
@Override
public TileEntity createTileEntity(World world, int metadata) {
if (teClass != null) {
try {
TileEntityEnder te = teClass.newInstance();
te.init();
return te;
} catch (Exception e) {
Log.error("Could not create tile entity for block " + name + " for class " + teClass);
}
}
return null;
}
@Override
@SideOnly(Side.CLIENT)
public void registerBlockIcons(IIconRegister iIconRegister) {
blockIcon = iIconRegister.registerIcon("enderio:" + name);
}
/* Subclass Helpers */
@Override
public boolean onBlockActivated(World world, int x, int y, int z, EntityPlayer entityPlayer, int side, float par7, float par8, float par9) {
TileEntity te = world.getTileEntity(x, y, z);
if (te instanceof ITankAccess) {
if (FluidUtil.fillInternalTankFromPlayerHandItem(world, x, y, z, entityPlayer, (ITankAccess) te)) {
return true;
}
if (FluidUtil.fillPlayerHandItemFromInternalTank(world, x, y, z, entityPlayer, (ITankAccess) te)) {
return true;
}
}
if (entityPlayer.isSneaking()) {
return false;
}
return openGui(world, x, y, z, entityPlayer, side);
}
protected boolean shouldWrench(World world, int x, int y, int z, EntityPlayer entityPlayer, int side) {
return true;
}
protected boolean openGui(World world, int x, int y, int z, EntityPlayer entityPlayer, int side) {
return false;
}
public boolean doNormalDrops(World world, int x, int y, int z) {
return true;
}
@Override
public boolean removedByPlayer(World world, EntityPlayer player, int x, int y, int z, boolean willHarvest) {
if (willHarvest) {
return true;
}
return super.removedByPlayer(world, player, x, y, z, willHarvest);
}
@Override
public void harvestBlock(World world, EntityPlayer player, int x, int y, int z, int meta) {
super.harvestBlock(world, player, x, y, z, meta);
world.setBlockToAir(x, y, z);
}
@Override
public final ArrayList<ItemStack> getDrops(World world, int x, int y, int z, int metadata, int fortune) {
if (doNormalDrops(world, x, y, z)) {
return super.getDrops(world, x, y, z, metadata, fortune);
}
return Lists.newArrayList(getNBTDrop(world, x, y, z, (TileEntityEnder) world.getTileEntity(x, y, z)));
}
public ItemStack getNBTDrop(World world, int x, int y, int z, TileEntityEnder te) {
int meta = damageDropped(te.getBlockMetadata());
ItemStack itemStack = new ItemStack(this, 1, meta);
processDrop(world, x, y, z, te, itemStack);
return itemStack;
}
protected void processDrop(World world, int x, int y, int z, @Nullable TileEntityEnder te, ItemStack drop) {
}
protected TileEntityEnder getTileEntityEio(World world, int x, int y, int z) {
TileEntity te = world.getTileEntity(x, y, z);
if (teClass.isInstance(te)) {
return (TileEntityEnder) te;
}
return null;
}
protected boolean shouldDoWorkThisTick(World world, int x, int y, int z, int interval) {
TileEntityEnder te = getTileEntityEio(world, x, y, z);
if (te == null) {
return world.getTotalWorldTime() % interval == 0;
} else {
return te.shouldDoWorkThisTick(interval);
}
}
protected boolean shouldDoWorkThisTick(World world, int x, int y, int z, int interval, int offset) {
TileEntityEnder te = getTileEntityEio(world, x, y, z);
if (te == null) {
return (world.getTotalWorldTime() + offset) % interval == 0;
} else {
return te.shouldDoWorkThisTick(interval, offset);
}
}
}
@@ -1,19 +1,23 @@
package com.enderio.core.common;
import net.minecraft.world.World;
import com.enderio.core.common.util.Scheduler;
public class CommonProxy
{
private static final Scheduler scheduler = new Scheduler();
public class CommonProxy {
private static final Scheduler scheduler = new Scheduler();
/**
* Returns a scheduler for the current side
* <p>
* For internal use only, please call {@link Scheduler#instance()} to obtain an
* {@link Scheduler} instance.
*/
public Scheduler getScheduler()
{
return scheduler;
}
/**
* Returns a scheduler for the current side
* <p>
* For internal use only, please call {@link Scheduler#instance()} to obtain
* an {@link Scheduler} instance.
*/
public Scheduler getScheduler() {
return scheduler;
}
public World getClientWorld() {
return null;
}
}
@@ -0,0 +1,153 @@
package com.enderio.core.common;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.network.NetworkManager;
import net.minecraft.network.Packet;
import net.minecraft.network.play.server.S35PacketUpdateTileEntity;
import net.minecraft.tileentity.TileEntity;
import com.enderio.core.api.common.util.IProgressTile;
import com.enderio.core.common.config.ConfigHandler;
import com.enderio.core.common.network.EnderPacketHandler;
import com.enderio.core.common.network.PacketProgress;
import com.enderio.core.common.util.BlockCoord;
import com.enderio.core.common.util.Util;
public abstract class TileEntityEnder extends TileEntity {
private final int checkOffset = (int) (Math.random() * 20);
private final boolean isProgressTile;
protected int lastProgressScaled = -1;
protected int ticksSinceLastProgressUpdate;
public TileEntityEnder() {
isProgressTile = this instanceof IProgressTile;
}
@Override
public final boolean canUpdate() {
return shouldUpdate() || isProgressTile;
}
protected boolean shouldUpdate() {
return true;
}
private long lastUpdate = 0;
@Override
public final void updateEntity() {
if (ConfigHandler.allowExternalTickSpeedup || worldObj.getTotalWorldTime() != lastUpdate) {
lastUpdate = worldObj.getTotalWorldTime();
doUpdate();
if (isProgressTile && !worldObj.isRemote) {
int curScaled = getProgressScaled(16);
if (++ticksSinceLastProgressUpdate >= getProgressUpdateFreq() || curScaled != lastProgressScaled) {
sendTaskProgressPacket();
lastProgressScaled = curScaled;
}
}
}
}
public final int getProgressScaled(int scale) {
if (isProgressTile) {
return Util.getProgressScaled(scale, (IProgressTile) this);
}
return 0;
}
protected void doUpdate() {
}
protected void sendTaskProgressPacket() {
if (isProgressTile) {
EnderPacketHandler.sendToAllAround(new PacketProgress((IProgressTile) this), this);
}
ticksSinceLastProgressUpdate = 0;
}
/**
* Controls how often progress updates. Has no effect if your TE is not
* {@link IProgressTile}.
*/
protected int getProgressUpdateFreq() {
return 20;
}
@Override
public final void readFromNBT(NBTTagCompound root) {
super.readFromNBT(root);
readCustomNBT(root);
}
@Override
public final void writeToNBT(NBTTagCompound root) {
super.writeToNBT(root);
writeCustomNBT(root);
}
@Override
public Packet getDescriptionPacket() {
NBTTagCompound tag = new NBTTagCompound();
writeCustomNBT(tag);
return new S35PacketUpdateTileEntity(xCoord, yCoord, zCoord, 1, tag);
}
@Override
public void onDataPacket(NetworkManager net, S35PacketUpdateTileEntity pkt) {
readCustomNBT(pkt.func_148857_g());
}
protected abstract void writeCustomNBT(NBTTagCompound root);
protected abstract void readCustomNBT(NBTTagCompound root);
protected void updateBlock() {
if (worldObj != null) {
worldObj.markBlockForUpdate(xCoord, yCoord, zCoord);
}
}
protected boolean isPoweredRedstone() {
return worldObj.getStrongestIndirectPower(xCoord, yCoord, zCoord) > 0;
}
/**
* Called directly after the TE is constructed. This is the place to call
* non-final methods.
*/
public void init() {
}
private BlockCoord cachedLocation = null;
public BlockCoord getLocation() {
return cachedLocation == null || !cachedLocation.equals(xCoord, yCoord, zCoord) ? (cachedLocation = new BlockCoord(this)) : cachedLocation;
}
/**
* Call this with an interval (in ticks) to find out if the current tick is
* the one you want to do some work. This is staggered so the work of
* different TEs is stretched out over time.
*
* @see #shouldDoWorkThisTick(int, int) If you need to offset work ticks
*/
protected boolean shouldDoWorkThisTick(int interval) {
return shouldDoWorkThisTick(interval, 0);
}
/**
* Call this with an interval (in ticks) to find out if the current tick is
* the one you want to do some work. This is staggered so the work of
* different TEs is stretched out over time.
*
* If you have different work items in your TE, use this variant to stagger
* your work.
*/
protected boolean shouldDoWorkThisTick(int interval, int offset) {
return (worldObj.getTotalWorldTime() + checkOffset + offset) % interval == 0;
}
}
@@ -21,157 +21,150 @@ import com.enderio.core.common.tweaks.Tweaks;
import net.minecraftforge.common.config.Configuration;
@Handler(value = HandlerType.FML, getInstFrom = Inst.METHOD)
public class ConfigHandler extends AbstractConfigHandler implements ITweakConfigHandler, IReloadCallback
{
private static final String sectionGeneral = Configuration.CATEGORY_GENERAL;
private static final String sectionEnchants = "enchants";
public class ConfigHandler extends AbstractConfigHandler implements ITweakConfigHandler, IReloadCallback {
private static final String sectionGeneral = Configuration.CATEGORY_GENERAL;
private static final String sectionEnchants = "enchants";
@Config
@Comment({"Show oredictionary names of every item in its tooltip.", "0 - Off", "1 - Always on", "2 - Only with shift", "3 - Only in debug mode"})
@Range(min = 0, max = 3)
@NoSync
public static int showOredictTooltips = 1;
@Config
@Comment({ "Show oredictionary names of every item in its tooltip.", "0 - Off", "1 - Always on", "2 - Only with shift", "3 - Only in debug mode" })
@Range(min = 0, max = 3)
@NoSync
public static int showOredictTooltips = 1;
@Config
@Comment({ "Show item registry names in tooltips.", "0 - Off", "1 - Always on", "2 - Only with shift", "3 - Only in debug mode" })
@Range(min = 0, max = 3)
@NoSync
public static int showRegistryNameTooltips = 3;
@Config
@Comment({ "Show durability on item tooltips.", "0 - Off", "1 - Always on", "2 - Only with shift", "3 - Only in debug mode" })
@Range(min = 0, max = 3)
@NoSync
public static int showDurabilityTooltips = 1;
@Config
@Comment({ "Show item registry names in tooltips.", "0 - Off", "1 - Always on", "2 - Only with shift", "3 - Only in debug mode" })
@Range(min = 0, max = 3)
@NoSync
public static int showRegistryNameTooltips = 3;
@Config
@Comment({ "Removes all void fog.", "0 = off", "1 = DEFAULT worldtype only", "2 = all world types" })
@NoSync
@Range(min = 0, max = 2)
public static int disableVoidFog = 1;
@Config
@Comment({ "Show durability on item tooltips.", "0 - Off", "1 - Always on", "2 - Only with shift", "3 - Only in debug mode" })
@Range(min = 0, max = 3)
@NoSync
public static int showDurabilityTooltips = 1;
@Config
@Comment("The max amount of XP levels an anvil recipe can use.")
public static int anvilMaxLevel = 40;
@Config
@Comment({ "Removes all void fog.", "0 = off", "1 = DEFAULT worldtype only", "2 = all world types" })
@NoSync
@Range(min = 0, max = 2)
public static int disableVoidFog = 1;
@Config
@Comment("The way the game should have been made (Yes this is the fireworks thing).")
public static boolean betterAchievements = true;
@Config
@Comment("The max amount of XP levels an anvil recipe can use.")
public static int anvilMaxLevel = 40;
@Config
@Comment("Disabling this option will prevent any crops added to the config json from being right clickable.")
public static boolean allowCropRC = true;
@Config
@Comment("The way the game should have been made (Yes this is the fireworks thing).")
public static boolean betterAchievements = true;
@Config
@Comment({ "0 - Do nothing", "1 - Remove stacktraces, leave 1-line missing texture errors", "2 - Remove all missing texture errors completely." })
@NoSync
@Range(min = 0, max = 2)
public static int textureErrorRemover = 1;
@Config
@Comment("Disabling this option will prevent any crops added to the config json from being right clickable.")
public static boolean allowCropRC = true;
@Config
@Comment({ "Controls the default sorting on the mod list GUI.", "0 - Default sort (load order)", "1 - A to Z sort", "2 - Z to A sort" })
@NoSync
@Range(min = 0, max = 2)
public static int defaultModSort = 1;
@Config
@Comment("Prevent tick speedup (i.e. torcherino) on any TE that uses the base TE class from EnderCore")
public static boolean allowExternalTickSpeedup = false;
@Config(sectionEnchants)
@Comment("Enchant ID for the XP boost enchant.")
@RestartReq(RestartReqs.REQUIRES_MC_RESTART)
@Range(min = 0, max = 255)
public static int enchantIDXPBoost = 45;
@Config
@Comment({ "0 - Do nothing", "1 - Remove stacktraces, leave 1-line missing texture errors", "2 - Remove all missing texture errors completely." })
@NoSync
@Range(min = 0, max = 2)
public static int textureErrorRemover = 1;
@Config(sectionEnchants)
@Comment("Allow the XP Boost enchant to be registered.")
@RestartReq(RestartReqs.REQUIRES_MC_RESTART)
public static boolean allowXPBoost = true;
@Config
@Comment({ "Controls the default sorting on the mod list GUI.", "0 - Default sort (load order)", "1 - A to Z sort", "2 - Z to A sort" })
@NoSync
@Range(min = 0, max = 2)
public static int defaultModSort = 1;
@Config(sectionEnchants)
@Comment("Enchant ID for the Auto Smelt enchant.")
@RestartReq(RestartReqs.REQUIRES_MC_RESTART)
@Range(min = 0, max = 255)
public static int enchantIDAutoSmelt = 46;
@Config(sectionEnchants)
@Comment("Enchant ID for the XP boost enchant.")
@RestartReq(RestartReqs.REQUIRES_MC_RESTART)
@Range(min = 0, max = 255)
public static int enchantIDXPBoost = 45;
@Config(sectionEnchants)
@Comment("Allow the Auto Smelt enchant to be registered.")
@RestartReq(RestartReqs.REQUIRES_MC_RESTART)
public static boolean allowAutoSmelt = true;
@Config(sectionEnchants)
@Comment("Allow the XP Boost enchant to be registered.")
@RestartReq(RestartReqs.REQUIRES_MC_RESTART)
public static boolean allowXPBoost = true;
@Config(sectionEnchants)
@Comment("Allow the Auto Smelt enchant to work with Fortune.")
public static boolean allowAutoSmeltWithFortune = true;
// @Config("test")
// public static List<Integer> test1 = Lists.newArrayList(1, 2, 3);
// @Config("test")
// public static List<Double> test2 = Lists.newArrayList(0.1, 0.2, 0.3);
// @Config("test")
// public static List<Boolean> test3 = Lists.newArrayList(true, false, true);
// @Config("test")
// public static List<String> test4 = Lists.newArrayList("test1", "test2", "test3");
@Config(sectionEnchants)
@Comment("Enchant ID for the Auto Smelt enchant.")
@RestartReq(RestartReqs.REQUIRES_MC_RESTART)
@Range(min = 0, max = 255)
public static int enchantIDAutoSmelt = 46;
private static ConfigHandler INSTANCE;
@Config(sectionEnchants)
@Comment("Allow the Auto Smelt enchant to be registered.")
@RestartReq(RestartReqs.REQUIRES_MC_RESTART)
public static boolean allowAutoSmelt = true;
public static File configFolder, enderConfigFolder;
public static File configFile;
public static ConfigProcessor processor;
@Config(sectionEnchants)
@Comment("Allow the Auto Smelt enchant to work with Fortune.")
public static boolean allowAutoSmeltWithFortune = true;
public static ConfigHandler instance()
{
if (INSTANCE == null)
{
INSTANCE = new ConfigHandler();
}
return INSTANCE;
// @Config("test")
// public static List<Integer> test1 = Lists.newArrayList(1, 2, 3);
// @Config("test")
// public static List<Double> test2 = Lists.newArrayList(0.1, 0.2, 0.3);
// @Config("test")
// public static List<Boolean> test3 = Lists.newArrayList(true, false, true);
// @Config("test")
// public static List<String> test4 = Lists.newArrayList("test1", "test2", "test3");
private static ConfigHandler INSTANCE;
public static File configFolder, enderConfigFolder;
public static File configFile;
public static ConfigProcessor processor;
public static ConfigHandler instance() {
if (INSTANCE == null) {
INSTANCE = new ConfigHandler();
}
return INSTANCE;
}
protected ConfigHandler()
{
super(EnderCore.MODID);
}
protected ConfigHandler() {
super(EnderCore.MODID);
}
@Override
public void init()
{
addSection(sectionGeneral);
addSection(sectionEnchants);
addSection("tweaks");
processor = new ConfigProcessor(getClass(), this, this);
processor.process(true);
}
@Override
public void init() {
addSection(sectionGeneral);
addSection(sectionEnchants);
addSection("tweaks");
processor = new ConfigProcessor(getClass(), this, this);
processor.process(true);
}
@Override
protected void reloadIngameConfigs()
{
Tweaks.loadIngameTweaks();
}
@Override
protected void reloadIngameConfigs() {
Tweaks.loadIngameTweaks();
}
@Override
protected void reloadNonIngameConfigs()
{
Tweaks.loadNonIngameTweaks();
}
@Override
protected void reloadNonIngameConfigs() {
Tweaks.loadNonIngameTweaks();
}
@Override
public void callback(ConfigProcessor inst)
{
Tweaks.loadIngameTweaks();
}
@Override
public void callback(ConfigProcessor inst) {
Tweaks.loadIngameTweaks();
}
@Override
public boolean addBooleanFor(Tweak tweak)
{
activateSection("tweaks");
return getValue(tweak.getName(), tweak.getComment(), true);
}
@Override
public boolean addBooleanFor(Tweak tweak) {
activateSection("tweaks");
return getValue(tweak.getName(), tweak.getComment(), true);
}
public void loadRightClickCrops()
{
JsonConfigReader<PlantInfo> reader = new JsonConfigReader<PlantInfo>(new ModToken(EnderCore.class, EnderCore.MODID.toLowerCase() + "/config"),
enderConfigFolder.getAbsolutePath() + "/cropConfig.json", PlantInfo.class);
for (PlantInfo i : reader)
{
i.init();
RightClickCropHandler.INSTANCE.addCrop(i);
}
public void loadRightClickCrops() {
JsonConfigReader<PlantInfo> reader = new JsonConfigReader<PlantInfo>(new ModToken(EnderCore.class, EnderCore.MODID.toLowerCase() + "/config"),
enderConfigFolder.getAbsolutePath() + "/cropConfig.json", PlantInfo.class);
for (PlantInfo i : reader) {
i.init();
RightClickCropHandler.INSTANCE.addCrop(i);
}
}
}
@@ -0,0 +1,30 @@
package com.enderio.core.common.network;
import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.zip.InflaterInputStream;
public class CompressedDataInput extends DataInputStream {
public CompressedDataInput(byte[] compressed) {
this(new ByteArrayInputStream(compressed));
}
public CompressedDataInput(InputStream in) {
super(new BufferedInputStream(new InflaterInputStream(in)));
}
public int readVariable() throws IOException {
int b, res = 0;
int shift = 0;
do {
b = readUnsignedByte();
res |= (b & 0x7F) << shift;
shift += 7;
} while(b >= 0x80);
return res;
}
}
@@ -0,0 +1,34 @@
package com.enderio.core.common.network;
import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.util.zip.DeflaterOutputStream;
public class CompressedDataOutput extends DataOutputStream {
final ByteArrayOutputStream baos;
public CompressedDataOutput() {
this(new ByteArrayOutputStream());
}
private CompressedDataOutput(ByteArrayOutputStream baos) {
super(new BufferedOutputStream(new DeflaterOutputStream(baos)));
this.baos = baos;
}
public void writeVariable(int value) throws IOException {
while((value & ~0x7F) != 0) {
writeByte(value | 0x80);
value >>= 7;
}
writeByte(value);
}
public byte[] getCompressed() throws IOException {
close();
return baos.toByteArray();
}
}
@@ -1,17 +1,33 @@
package com.enderio.core.common.network;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.tileentity.TileEntity;
import com.enderio.core.EnderCore;
import com.enderio.core.common.config.PacketConfigSync;
import cpw.mods.fml.common.network.NetworkRegistry.TargetPoint;
import cpw.mods.fml.common.network.simpleimpl.IMessage;
import cpw.mods.fml.common.network.simpleimpl.SimpleNetworkWrapper;
import cpw.mods.fml.relauncher.Side;
public class EnderPacketHandler
{
public static final SimpleNetworkWrapper INSTANCE = new SimpleNetworkWrapper(EnderCore.NAME);
public class EnderPacketHandler {
public static final SimpleNetworkWrapper INSTANCE = new SimpleNetworkWrapper(EnderCore.NAME);
static
{
INSTANCE.registerMessage(PacketConfigSync.class, PacketConfigSync.class, 0, Side.CLIENT);
}
static {
INSTANCE.registerMessage(PacketConfigSync.class, PacketConfigSync.class, 0, Side.CLIENT);
INSTANCE.registerMessage(PacketProgress.class, PacketProgress.class, 1, Side.CLIENT);
}
public static void sendToAllAround(IMessage message, TileEntity te, int range) {
INSTANCE.sendToAllAround(message, new TargetPoint(te.getWorldObj().provider.dimensionId, te.xCoord, te.yCoord, te.zCoord, range));
}
public static void sendToAllAround(IMessage message, TileEntity te) {
sendToAllAround(message, te, 64);
}
public static void sendTo(IMessage message, EntityPlayerMP player) {
INSTANCE.sendTo(message, player);
}
}
@@ -0,0 +1,69 @@
package com.enderio.core.common.network;
import io.netty.buffer.ByteBuf;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.world.World;
import com.enderio.core.common.util.Log;
import cpw.mods.fml.common.network.ByteBufUtils;
import cpw.mods.fml.common.network.simpleimpl.IMessage;
import cpw.mods.fml.common.network.simpleimpl.MessageContext;
public abstract class MessageTileEntity<T extends TileEntity> implements IMessage {
protected int x;
protected int y;
protected int z;
private Class<? extends TileEntity> tileClass;
protected MessageTileEntity() {
}
protected MessageTileEntity(T tile) {
tileClass = tile.getClass();
x = tile.xCoord;
y = tile.yCoord;
z = tile.zCoord;
}
public void toBytes(ByteBuf buf) {
buf.writeInt(x);
buf.writeInt(y);
buf.writeInt(z);
ByteBufUtils.writeUTF8String(buf, tileClass.getName());
}
@Override
public void fromBytes(ByteBuf buf) {
x = buf.readInt();
y = buf.readInt();
z = buf.readInt();
String str = ByteBufUtils.readUTF8String(buf);
try {
tileClass = (Class<TileEntity>) Class.forName(str);
} catch (Exception e) {
Log.error("AbstractPacketTileEntity could not load tile entity class: " + str);
}
}
protected T getTileEntity(World worldObj) {
if(worldObj == null) {
return null;
}
TileEntity te = worldObj.getTileEntity(x, y, z);
if(te == null) {
return null;
}
if(tileClass.isAssignableFrom(te.getClass())) {
return (T) te;
}
return null;
}
protected World getWorld(MessageContext ctx) {
return ctx.getServerHandler().playerEntity.worldObj;
}
}
@@ -0,0 +1,79 @@
package com.enderio.core.common.network;
import io.netty.buffer.ByteBuf;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.world.World;
import com.enderio.core.common.util.Log;
import cpw.mods.fml.common.network.simpleimpl.IMessage;
import cpw.mods.fml.common.network.simpleimpl.IMessageHandler;
import cpw.mods.fml.common.network.simpleimpl.MessageContext;
/**
* Created by CrazyPants on 27/02/14.
*/
public class MessageTileNBT implements IMessage, IMessageHandler<MessageTileNBT, IMessage> {
TileEntity te;
int x;
int y;
int z;
NBTTagCompound tags;
boolean renderOnUpdate = false;
public MessageTileNBT() {
}
public MessageTileNBT(TileEntity te) {
this.te = te;
x = te.xCoord;
y = te.yCoord;
z = te.zCoord;
tags = new NBTTagCompound();
te.writeToNBT(tags);
}
@Override
public void toBytes(ByteBuf buffer) {
buffer.writeInt(x);
buffer.writeInt(y);
buffer.writeInt(z);
NetworkUtil.writeNBTTagCompound(tags, buffer);
}
@Override
public void fromBytes(ByteBuf dis) {
x = dis.readInt();
y = dis.readInt();
z = dis.readInt();
tags = NetworkUtil.readNBTTagCompound(dis);
}
@Override
public IMessage onMessage(MessageTileNBT msg, MessageContext ctx) {
te = handle(ctx.getServerHandler().playerEntity.worldObj);
if(te != null && renderOnUpdate) {
te.getWorldObj().markBlockForUpdate(x,y,z);
}
return null;
}
private TileEntity handle(World world) {
if(world == null) {
Log.warn("PacketUtil.handleTileEntityPacket: TileEntity null world processing tile entity packet.");
return null;
}
TileEntity te = world.getTileEntity(x, y, z);
if(te == null) {
Log.warn("PacketUtil.handleTileEntityPacket: TileEntity null when processing tile entity packet.");
return null;
}
te.readFromNBT(tags);
return te;
}
}
@@ -0,0 +1,58 @@
package com.enderio.core.common.network;
import io.netty.buffer.ByteBuf;
import java.io.IOException;
import net.minecraft.nbt.CompressedStreamTools;
import net.minecraft.nbt.NBTSizeTracker;
import net.minecraft.nbt.NBTTagCompound;
import cpw.mods.fml.common.FMLCommonHandler;
/**
* Created by CrazyPants on 27/02/14.
*/
public class NetworkUtil {
public static NBTTagCompound readNBTTagCompound(ByteBuf dataIn) {
try {
short size = dataIn.readShort();
if(size < 0) {
return null;
} else {
byte[] buffer = new byte[size];
dataIn.readBytes(buffer);
return CompressedStreamTools.func_152457_a(buffer, NBTSizeTracker.field_152451_a);
}
} catch (IOException e) {
FMLCommonHandler.instance().raiseException(e, "Custom Packet", true);
return null;
}
}
public static void writeNBTTagCompound(NBTTagCompound compound, ByteBuf dataout) {
try {
if(compound == null) {
dataout.writeShort(-1);
} else {
byte[] buf = CompressedStreamTools.compress(compound);
dataout.writeShort((short) buf.length);
dataout.writeBytes(buf);
}
} catch (IOException e) {
FMLCommonHandler.instance().raiseException(e, "PacketUtil.readTileEntityPacket.writeNBTTagCompound", true);
}
}
public static byte[] readByteArray(ByteBuf buf) {
int size = buf.readMedium();
byte[] res = new byte[size];
buf.readBytes(res);
return res;
}
public static void writeByteArray(ByteBuf buf, byte[] arr) {
buf.writeMedium(arr.length);
buf.writeBytes(arr);
}
}
@@ -0,0 +1,48 @@
package com.enderio.core.common.network;
import io.netty.buffer.ByteBuf;
import net.minecraft.tileentity.TileEntity;
import com.enderio.core.EnderCore;
import com.enderio.core.api.common.util.IProgressTile;
import cpw.mods.fml.common.network.simpleimpl.IMessage;
import cpw.mods.fml.common.network.simpleimpl.IMessageHandler;
import cpw.mods.fml.common.network.simpleimpl.MessageContext;
public class PacketProgress extends MessageTileEntity<TileEntity> implements IMessageHandler<PacketProgress, IMessage> {
private float progress;
public PacketProgress() {
}
public PacketProgress(IProgressTile tile) {
super(tile.getTileEntity());
progress = tile.getProgress();
if (progress == 0) {
progress = -1;
}
}
@Override
public void toBytes(ByteBuf buf) {
super.toBytes(buf);
buf.writeFloat(progress);
}
@Override
public void fromBytes(ByteBuf buf) {
super.fromBytes(buf);
progress = buf.readFloat();
}
@Override
public IMessage onMessage(PacketProgress message, MessageContext ctx) {
TileEntity tile = message.getTileEntity(EnderCore.proxy.getClientWorld());
if(tile instanceof IProgressTile) {
((IProgressTile) tile).setProgress(message.progress);
}
return null;
}
}
@@ -5,6 +5,9 @@ import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.enderio.core.api.common.util.IFluidReceptor;
import com.enderio.core.api.common.util.ITankAccess;
import net.minecraft.block.Block;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.init.Items;
@@ -4,6 +4,8 @@ import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import com.enderio.core.api.common.util.IItemReceptor;
import net.minecraft.block.Block;
import net.minecraft.entity.item.EntityItem;
import net.minecraft.inventory.IInventory;
@@ -24,6 +24,7 @@ import net.minecraft.world.World;
import net.minecraftforge.common.util.ForgeDirection;
import net.minecraftforge.oredict.OreDictionary;
import com.enderio.core.api.common.util.IProgressTile;
import com.enderio.core.common.vecmath.Vector3d;
import com.google.common.base.Charsets;
import com.google.common.base.Joiner;