Skip to content

Instantly share code, notes, and snippets.

@azatoth
Created January 5, 2014 19:54
Show Gist options
  • Save azatoth/8272943 to your computer and use it in GitHub Desktop.
Save azatoth/8272943 to your computer and use it in GitHub Desktop.
Trial to make a IC2 addon (code layout idea borrowed from Advanced Machines)
package azatoth.mc.recycler;
import ic2.api.item.Items;
import azatoth.mc.recycler.client.ClientProxy;
import net.minecraft.block.BlockContainer;
import net.minecraft.block.material.Material;
import net.minecraft.client.Minecraft;
import net.minecraft.client.entity.EntityPlayerSP;
import net.minecraft.client.renderer.texture.IconRegister;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.network.packet.Packet3Chat;
import net.minecraft.server.MinecraftServer;
import net.minecraft.src.ModLoader;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.Icon;
import net.minecraft.util.MathHelper;
import net.minecraft.world.IBlockAccess;
import net.minecraft.world.World;
import net.minecraftforge.client.MinecraftForgeClient;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.common.network.ForgePacket;
import net.minecraftforge.common.network.ForgePacketHandler;
import net.minecraftforge.event.ForgeEventFactory;
import net.minecraftforge.oredict.OreDictionary;
import net.minecraftforge.server.command.ForgeCommand;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
public class BlockRecyclerBlock extends BlockContainer {
int[][] sideAndFacingToSpriteOffset = new int[][] {
{ 3, 2, 0, 0, 0, 0 },
{ 2, 3, 1, 1, 1, 1 },
{ 1, 1, 3, 2, 5, 4 },
{ 0, 0, 2, 3, 4, 5 },
{ 4, 5, 4, 5, 3, 2 },
{ 5, 4, 5, 4, 2, 3 } };
@Override
public boolean onBlockActivated(World world, int x, int y,
int z, EntityPlayer entityPlayer, int par6, float par7,
float par8, float par9) {
entityPlayer.addChatMessage(par6 + " " + par7 + " " + par8 + " " + par9 );
if(entityPlayer.isSneaking()) {
return false;
}
return false;
}
@SideOnly(Side.CLIENT)
private Icon[] icons;
public BlockRecyclerBlock(int id) {
super(id, Material.iron);
this.setCreativeTab(CreativeTabs.tabRedstone);
this.setUnlocalizedName("RecyclerBlock");
this.setHardness(2.0F);
this.setStepSound(soundMetalFootstep);
}
@Override
public TileEntity createNewTileEntity(World world) {
return null;
}
@Override
public TileEntity createTileEntity(World world, int metadata) {
return new TileEntityRecyclerBlock();
}
@SideOnly(Side.CLIENT)
@Override
public Icon getBlockTexture(IBlockAccess world, int x, int y, int z,
int side) {
System.out.format("getBlockTexture(x=%d,y=%d,z=%d,side=%d)\n", x,y,z,side);
TileEntity te = world.getBlockTileEntity(x, y, z);
if (te instanceof TileEntityBlock) {
TileEntityBlock teb = (TileEntityBlock) te;
int facing = teb.getFacing();
return icons[sideAndFacingToSpriteOffset[side][facing]];
} else {
return null;
}
}
@SideOnly(Side.CLIENT)
@Override
public Icon getIcon(int blockSide, int blockMeta) {
return icons[sideAndFacingToSpriteOffset[blockSide][3]];
}
@Override
public void onBlockPlacedBy(World world, int x, int y, int z,
EntityLivingBase entityLiving, ItemStack itemStack) {
byte facing = 0;
if(entityLiving instanceof EntityPlayer) {
EntityPlayer ep = (EntityPlayer) entityLiving;
ep.addChatMessage("rotationYaw: " +
ep.rotationYaw + " " +
ep.rotationYaw * 4F / 360F + .5d + " " +
MathHelper
.floor_double(entityLiving.rotationYaw * 4F / 360F + .5D) + " " +
(MathHelper
.floor_double(entityLiving.rotationYaw * 4F / 360F + .5D) & 3)
);
}
int direction = MathHelper
.floor_double(entityLiving.rotationYaw * 4F / 360F + .5D) & 3;
switch (direction) {
case 0:
facing = 2;
break;
case 1:
facing = 5;
break;
case 2:
facing = 3;
break;
case 3:
facing = 4;
break;
}
TileEntity te = world.getBlockTileEntity(x, y, z);
if (te != null && te instanceof TileEntityBlock) {
TileEntityBlock terb = (TileEntityBlock) te;
terb.setFacing(facing);
world.markBlockForUpdate(x, y, z);
}
}
@SideOnly(Side.CLIENT)
@Override
public void registerIcons(IconRegister par1IconRegister) {
/*
icons = new Icon[] {
par1IconRegister.registerIcon(Recycler.modid + ':' + "5plate"), // bottom
par1IconRegister.registerIcon(Recycler.modid + ':' + "5plate"), // top
par1IconRegister.registerIcon(Recycler.modid + ':' + "1plate"), // north
par1IconRegister.registerIcon(Recycler.modid + ':' + "front"), // east
par1IconRegister.registerIcon(Recycler.modid + ':' + "4plate"), // south
par1IconRegister.registerIcon(Recycler.modid + ':' + "4plate"), // west
};*/
icons = new Icon[] {
par1IconRegister.registerIcon(Recycler.modid + ':' + "down"), // 0
par1IconRegister.registerIcon(Recycler.modid + ':' + "up"), // 1
par1IconRegister.registerIcon(Recycler.modid + ':' + "back"), // 2
par1IconRegister.registerIcon(Recycler.modid + ':' + "front"), // 3
par1IconRegister.registerIcon(Recycler.modid + ':' + "right"), // 4
par1IconRegister.registerIcon(Recycler.modid + ':' + "left"), // 5
};
}
}
package azatoth.mc.recycler;
import net.minecraft.block.Block;
import net.minecraftforge.common.Configuration;
import cpw.mods.fml.common.Mod;
import cpw.mods.fml.common.Mod.EventHandler;
import cpw.mods.fml.common.Mod.Instance;
import cpw.mods.fml.common.SidedProxy;
import cpw.mods.fml.common.event.FMLInitializationEvent;
import cpw.mods.fml.common.event.FMLPostInitializationEvent;
import cpw.mods.fml.common.event.FMLPreInitializationEvent;
import cpw.mods.fml.common.network.NetworkMod;
import cpw.mods.fml.common.registry.GameRegistry;
import cpw.mods.fml.common.registry.LanguageRegistry;
@Mod(modid = Recycler.modid, name = "Recycler", version = "0.0.1", dependencies = "required-after:IC2@2.0.225")
@NetworkMod(clientSideRequired = true, serverSideRequired = false)
public class Recycler implements IProxy {
@Instance(value = Recycler.modid)
public static Recycler instance;
public static final String modid = "AzaToth_Recycler";
@SidedProxy(clientSide = "azatoth.mc.recycler.client.ClientProxy", serverSide = "azatoth.mc.recycler.Recycler")
public static IProxy proxy;
public static Block recyclerBlock;
private Configuration config;
@Override
public void load() {
// NOOP
}
@EventHandler
public void load(FMLInitializationEvent event) {
GameRegistry.registerTileEntity(TileEntityRecyclerBlock.class, modid
+ "RecyclerBlockEntitiy");
LanguageRegistry.addName(recyclerBlock, "Recycler");
proxy.load();
}
@EventHandler
public void postInit(FMLPostInitializationEvent evt) {
config.save();
}
@EventHandler
public void preInit(FMLPreInitializationEvent evt) {
instance = this;
config = new Configuration(evt.getSuggestedConfigurationFile());
config.load();
recyclerBlock = new BlockRecyclerBlock(config.getBlock("RecyclerBlock",
1337).getInt()).setUnlocalizedName("recyclerBlock");
GameRegistry.registerBlock(recyclerBlock, modid
+ recyclerBlock.getUnlocalizedName().substring(5));
}
}
package azatoth.mc.recycler;
import net.minecraft.tileentity.TileEntity;
import net.minecraftforge.common.ForgeDirection;
import net.minecraftforge.common.MinecraftForge;
import ic2.api.energy.event.EnergyTileLoadEvent;
import ic2.api.energy.event.EnergyTileUnloadEvent;
import ic2.api.energy.tile.IEnergySink;
public abstract class TileEntityBaseMachine extends TileEntityBlock implements IEnergySink {
public double energy = 0D;
public double maxEnergy = 10000D;
private boolean addedToEnergyNet;
public TileEntityBaseMachine() {
super();
}
@Override
public boolean acceptsEnergyFrom(TileEntity emitter, ForgeDirection direction) {
return true;
}
@Override
public double demandedEnergyUnits() {
return maxEnergy - energy;
}
@Override
public int getMaxSafeInput() {
return 32;
}
@Override
public double injectEnergyUnits(ForgeDirection directionFrom, double amount) {
energy += amount;
if (energy >= maxEnergy) {
double excess = energy - maxEnergy;
energy = maxEnergy;
return excess;
} else {
return 0;
}
}
@Override
public void invalidate() {
EnergyTileUnloadEvent unloadEvent = new EnergyTileUnloadEvent(this);
MinecraftForge.EVENT_BUS.post(unloadEvent);
}
@Override
public void updateEntity()
{
if (!worldObj.isRemote && !addedToEnergyNet) {
EnergyTileLoadEvent loadEvent = new EnergyTileLoadEvent(this);
MinecraftForge.EVENT_BUS.post(loadEvent);
addedToEnergyNet = true;
}
super.updateEntity();
}
}
package azatoth.mc.recycler;
import java.util.ArrayList;
import java.util.List;
import ic2.api.network.INetworkDataProvider;
import ic2.api.network.NetworkHelper;
import ic2.api.tile.IWrenchable;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.tileentity.TileEntity;
public abstract class TileEntityBlock extends TileEntity implements IWrenchable,
INetworkDataProvider {
public static List<String> networkedFields;
public boolean active;
public short facing;
public short prevFacing;
protected boolean initialized;
public TileEntityBlock() {
if (networkedFields == null) {
networkedFields = new ArrayList<String>();
networkedFields.add("active");
networkedFields.add("facing");
}
}
public boolean getActive() {
return active;
}
@Override
public short getFacing() {
return this.facing;
}
@Override
public List<String> getNetworkedFields() {
return networkedFields;
}
@Override
public ItemStack getWrenchDrop(EntityPlayer entityPlayer) {
int id = worldObj.getBlockId(xCoord, yCoord, zCoord);
int metadata = worldObj.getBlockMetadata(xCoord, yCoord, zCoord);
return new ItemStack(id, 1, metadata);
}
@Override
public float getWrenchDropRate() {
return 1.0f;
}
@Override
public void readFromNBT(NBTTagCompound par1nbtTagCompound) {
super.readFromNBT(par1nbtTagCompound);
this.prevFacing = this.facing = par1nbtTagCompound.getShort("facing");
}
public void setActive(boolean active) {
this.active = active;
}
@Override
public void setFacing(short side) {
this.facing = side;
NetworkHelper.updateTileEntityField(this, "facing");
this.prevFacing = side;
NetworkHelper.announceBlockUpdate(worldObj, xCoord, yCoord, zCoord);
worldObj.markBlockForRenderUpdate(xCoord, yCoord, zCoord);
worldObj.markBlockForUpdate(xCoord, yCoord, zCoord);
worldObj.notifyBlockChange(xCoord, yCoord, zCoord, worldObj.getBlockId(xCoord, yCoord, zCoord));
System.out.format("Announced block update at %d %d %d; Block there is %s\n", xCoord, yCoord, zCoord, worldObj.blockExists(xCoord, yCoord, zCoord));
}
@Override
public void updateEntity() {
if (!this.initialized) {
this.initialized = true;
NetworkHelper.announceBlockUpdate(worldObj, xCoord, yCoord, zCoord);
}
}
@Override
public boolean wrenchCanRemove(EntityPlayer entityPlayer) {
return true;
}
@Override
public boolean wrenchCanSetFacing(EntityPlayer entityPlayer, int side) {
if (side < 0 || side == facing)
return false;
return true;
}
@Override
public void writeToNBT(NBTTagCompound par1nbtTagCompound) {
super.writeToNBT(par1nbtTagCompound);
par1nbtTagCompound.setShort("facing", this.facing);
}
}
package azatoth.mc.recycler;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.IInventory;
import net.minecraft.item.ItemStack;
import net.minecraft.tileentity.TileEntity;
public abstract class TileEntityMachine extends TileEntityBlock implements
IInventory {
private static final double MAX_DISTANCE = 64.0D;
public ItemStack[] inventory;
@Override
public void closeChest() {}
@Override
public ItemStack decrStackSize(int i, int j) {
if (inventory[i] != null) {
ItemStack returnStack;
if (inventory[i].stackSize <= j) {
returnStack = inventory[i];
inventory[i] = null;
return returnStack;
} else {
returnStack = inventory[i].splitStack(j);
if (inventory[i].stackSize == 0) {
inventory[i] = null;
}
return returnStack;
}
}
return null;
}
@Override
public int getInventoryStackLimit() {
return 64;
}
@Override
public abstract String getInvName();
@Override
public int getSizeInventory() {
return inventory.length;
}
@Override
public ItemStack getStackInSlot(int i) {
return inventory[i];
}
@Override
public ItemStack getStackInSlotOnClosing(int i) {
return null;
}
@Override
public boolean isInvNameLocalized() {
return false;
}
@Override
public boolean isItemValidForSlot(int i, ItemStack itemstack) {
return false;
}
@Override
public boolean isUseableByPlayer(EntityPlayer entityplayer) {
TileEntity blockTileEntity = worldObj.getBlockTileEntity(xCoord,
yCoord, zCoord);
if (blockTileEntity == this) {
double dx = xCoord + 0.5D;
double dy = yCoord + 0.5D;
double dz = zCoord + 0.5D;
return entityplayer.getDistance(dx, dy, dz) <= MAX_DISTANCE;
} else {
return false;
}
}
@Override
public void openChest() {}
@Override
public void setInventorySlotContents(int i, ItemStack itemstack) {
inventory[i] = itemstack;
if (itemstack != null && itemstack.stackSize > getInventoryStackLimit()) {
itemstack.stackSize = getInventoryStackLimit();
}
}
}
package azatoth.mc.recycler;
import java.util.ArrayList;
import ic2.api.energy.event.EnergyTileLoadEvent;
import ic2.api.energy.tile.IEnergySink;
import ic2.api.network.INetworkClientTileEntityEventListener;
import ic2.api.network.INetworkDataProvider;
import ic2.api.network.NetworkHelper;
import ic2.api.tile.IWrenchable;
import net.minecraft.inventory.IInventory;
import net.minecraft.inventory.ISidedInventory;
import net.minecraft.util.MathHelper;
import net.minecraftforge.common.MinecraftForge;
public class TileEntityRecyclerBlock extends TileEntityBaseMachine {
@Override
public void updateEntity() {
super.updateEntity();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment