Skip to content

Instantly share code, notes, and snippets.

@matyklug18
Last active September 8, 2019 11:24
Show Gist options
  • Save matyklug18/9929f8c981593f0f5ed781f0c0571d8b to your computer and use it in GitHub Desktop.
Save matyklug18/9929f8c981593f0f5ed781f0c0571d8b to your computer and use it in GitHub Desktop.
TileEntity for my ore
package matyk.aqarium2.randomores;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Random;
import org.json.simple.JSONObject;
import org.lwjgl.util.Color;
import matyk.aqarium2.main.Main;
import net.minecraft.block.Block;
import net.minecraft.block.material.Material;
import net.minecraft.block.state.IBlockState;
import net.minecraft.client.renderer.block.model.ModelResourceLocation;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.init.Items;
import net.minecraft.item.Item;
import net.minecraft.item.ItemBlock;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.BlockRenderLayer;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import net.minecraftforge.client.model.ModelLoader;
import net.minecraftforge.fml.common.registry.ForgeRegistries;
public class RandomOreBlock extends Block{
public RandomOreBlock(Material materialIn, int hardness, int resistance, CreativeTabs tab) {
super(materialIn);
this.blockHardness = hardness;
this.blockResistance = resistance;
this.setCreativeTab(tab);
}
String name;
@Override
public boolean hasTileEntity(IBlockState state) {
return true;
}
//RandomOreTile tile;
/*@Override
public Item getItemDropped(IBlockState state, Random rand, int fortune) {
if(tile.getColor().getRed() > 127) {
return Items.APPLE;
} else
return Items.BAKED_POTATO;
}*/
@Override
public TileEntity createTileEntity(World world, IBlockState state) {
return new RandomOreTile();
}
public void register(String name) {
this.name = name.toLowerCase();
ForgeRegistries.BLOCKS.register(this.setRegistryName(name.toLowerCase()).setUnlocalizedName(name));
ForgeRegistries.ITEMS.register(new ItemBlock(this).setRegistryName(name.toLowerCase()).setUnlocalizedName(name));
}
public void registerItemTexture() {
ModelLoader.setCustomModelResourceLocation(Item.getItemFromBlock(this), 0, new ModelResourceLocation(Main.MODID + ":" + name, "inventory"));
}
@Override
public BlockRenderLayer getBlockLayer() {
return BlockRenderLayer.CUTOUT;
}
public void setFiles() {
JSONObject blockstatejson = new JSONObject();
JSONObject blockstatevariants = new JSONObject();
JSONObject blockstatenormal = new JSONObject();
blockstatenormal.put("model", Main.MODID + ":" + name);
blockstatevariants.put("normal", blockstatenormal);
blockstatejson.put("variants", blockstatevariants);
File blockstatesfile = new File("../src/main/resources/assets/" + Main.MODID + "/blockstates/" + name + ".json");
try {
blockstatesfile.createNewFile();
FileWriter blockstateswriter = new FileWriter(blockstatesfile);
BufferedWriter blockstatesbwriter = new BufferedWriter(blockstateswriter);
blockstatesbwriter.write(blockstatejson.toString().replace("\\", ""));
blockstatesbwriter.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
JSONObject blockmodeljson = new JSONObject();
blockmodeljson.put("parent", "block/leaves");
JSONObject blockmodeltextures = new JSONObject();
blockmodeltextures.put("all", Main.MODID + ":" + "blocks/" + name);
blockmodeljson.put("textures", blockmodeltextures);
File blockmodelsfile = new File("../src/main/resources/assets/" + Main.MODID + "/models/block/" + name + ".json");
try {
blockmodelsfile.createNewFile();
FileWriter blockmodelswriter = new FileWriter(blockmodelsfile);
BufferedWriter blockmodelsbwriter = new BufferedWriter(blockmodelswriter);
blockmodelsbwriter.write(blockmodeljson.toString().replace("\\", ""));
blockmodelsbwriter.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
JSONObject itemmodeljson = new JSONObject();
itemmodeljson.put("parent", Main.MODID + ":" + "block/" + name);
File itemmodelsfile = new File("../src/main/resources/assets/" + Main.MODID + "/models/item/" + name + ".json");
try {
itemmodelsfile.createNewFile();
FileWriter itemmodelswriter = new FileWriter(itemmodelsfile);
BufferedWriter itemmodelsbwriter = new BufferedWriter(itemmodelswriter);
itemmodelsbwriter.write(itemmodeljson.toString().replace("\\", ""));
itemmodelsbwriter.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
package matyk.aqarium2.randomores;
import java.util.Random;
import org.lwjgl.util.Color;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.network.NetworkManager;
import net.minecraft.network.play.server.SPacketUpdateTileEntity;
import net.minecraft.tileentity.TileEntity;
public class RandomOreTile extends TileEntity{
Random rand = new Random();
Color color;
public RandomOreTile() {
color = new Color(rand.nextInt(255), rand.nextInt(255), rand.nextInt(255));
markDirty();
}
public Color getColor() {
return color;
}
@Override
public SPacketUpdateTileEntity getUpdatePacket(){
NBTTagCompound nbtTag = new NBTTagCompound();
//Write your data into the nbtTag
nbtTag.setInteger("red", color.getRed());
nbtTag.setInteger("green", color.getGreen());
nbtTag.setInteger("blue", color.getBlue());
return new SPacketUpdateTileEntity(getPos(), 1, nbtTag);
}
@Override
public void onDataPacket(NetworkManager net, SPacketUpdateTileEntity pkt){
NBTTagCompound tag = pkt.getNbtCompound();
//Handle your Data
color = new Color(
tag.getInteger("red"),
tag.getInteger("green"),
tag.getInteger("blue"));
}
@Override
public NBTTagCompound writeToNBT(NBTTagCompound compound) {
super.writeToNBT(compound);
compound.setInteger("red", color.getRed());
compound.setInteger("green", color.getGreen());
compound.setInteger("blue", color.getBlue());
markDirty();
return compound;
}
@Override
public void readFromNBT(NBTTagCompound compound) {
super.readFromNBT(compound);
color = new Color(
compound.getInteger("red"),
compound.getInteger("green"),
compound.getInteger("blue"));
markDirty();
}
@Override
public NBTTagCompound getUpdateTag() {
super.getUpdateTag();
NBTTagCompound cmpd = new NBTTagCompound();
cmpd.setInteger("red", color.getRed());
cmpd.setInteger("green", color.getGreen());
cmpd.setInteger("blue", color.getBlue());
markDirty();
return cmpd;
}
@Override
public void handleUpdateTag(NBTTagCompound tag) {
super.handleUpdateTag(tag);
color = new Color(
tag.getInteger("red"),
tag.getInteger("green"),
tag.getInteger("blue"));
markDirty();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment