Add first tile entity
This commit is contained in:
parent
5c192d9e19
commit
1d9b32075a
33
src/main/java/com/pjht/ssspcore/block/BlockTileEntity.java
Normal file
33
src/main/java/com/pjht/ssspcore/block/BlockTileEntity.java
Normal file
@ -0,0 +1,33 @@
|
||||
package com.pjht.ssspcore.block;
|
||||
|
||||
import net.minecraft.block.material.Material;
|
||||
import net.minecraft.block.state.IBlockState;
|
||||
import net.minecraft.tileentity.TileEntity;
|
||||
import net.minecraft.util.math.BlockPos;
|
||||
import net.minecraft.world.IBlockAccess;
|
||||
import net.minecraft.world.World;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
public abstract class BlockTileEntity<TE extends TileEntity> extends BlockBase {
|
||||
|
||||
public BlockTileEntity(Material material, String name) {
|
||||
super(material, name);
|
||||
}
|
||||
|
||||
public abstract Class<TE> getTileEntityClass();
|
||||
|
||||
public TE getTileEntity(IBlockAccess world, BlockPos pos) {
|
||||
return (TE)world.getTileEntity(pos);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasTileEntity(IBlockState state) {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public abstract TE createTileEntity(World world, IBlockState state);
|
||||
|
||||
}
|
@ -1,29 +1,35 @@
|
||||
package com.pjht.ssspcore.block;
|
||||
|
||||
import com.pjht.ssspcore.block.te.counter.BlockCounter;
|
||||
|
||||
import net.minecraft.block.Block;
|
||||
import net.minecraft.item.Item;
|
||||
import net.minecraftforge.fml.common.registry.GameRegistry;
|
||||
import net.minecraftforge.registries.IForgeRegistry;
|
||||
|
||||
public class ModBlocks {
|
||||
public static BlockOre oreCopper = new BlockOre("ore_copper","oreCopper");
|
||||
public static BlockPedestal pedestal = new BlockPedestal();
|
||||
public static BlockCounter counter = new BlockCounter();
|
||||
|
||||
public static void register(IForgeRegistry<Block> registry) {
|
||||
registry.registerAll(
|
||||
oreCopper,
|
||||
pedestal
|
||||
);
|
||||
GameRegistry.registerTileEntity(counter.getTileEntityClass(), counter.getRegistryName().toString());
|
||||
}
|
||||
|
||||
public static void registerItemBlocks(IForgeRegistry<Item> registry) {
|
||||
registry.registerAll(
|
||||
oreCopper.createItemBlock(),
|
||||
pedestal.createItemBlock()
|
||||
pedestal.createItemBlock(),
|
||||
counter.createItemBlock()
|
||||
);
|
||||
}
|
||||
|
||||
public static void registerItemModels() {
|
||||
oreCopper.registerItemModel(Item.getItemFromBlock(oreCopper));
|
||||
pedestal.registerItemModel(Item.getItemFromBlock(pedestal));
|
||||
counter.registerItemModel(Item.getItemFromBlock(counter));
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,46 @@
|
||||
package com.pjht.ssspcore.block.te.counter;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
import com.pjht.ssspcore.block.BlockTileEntity;
|
||||
|
||||
import net.minecraft.block.material.Material;
|
||||
import net.minecraft.block.state.IBlockState;
|
||||
import net.minecraft.entity.player.EntityPlayer;
|
||||
import net.minecraft.util.EnumFacing;
|
||||
import net.minecraft.util.EnumHand;
|
||||
import net.minecraft.util.math.BlockPos;
|
||||
import net.minecraft.util.text.TextComponentString;
|
||||
import net.minecraft.world.World;
|
||||
|
||||
public class BlockCounter extends BlockTileEntity<TileEntityCounter> {
|
||||
|
||||
public BlockCounter() {
|
||||
super(Material.ROCK, "counter");
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onBlockActivated(World world, BlockPos pos, IBlockState state, EntityPlayer player, EnumHand hand, EnumFacing side, float hitX, float hitY, float hitZ) {
|
||||
if (!world.isRemote) {
|
||||
TileEntityCounter tile = getTileEntity(world, pos);
|
||||
if (side == EnumFacing.DOWN) {
|
||||
tile.decrementCount();
|
||||
} else if (side == EnumFacing.UP) {
|
||||
tile.incrementCount();
|
||||
}
|
||||
player.sendMessage(new TextComponentString("Count: " + tile.getCount()));
|
||||
}
|
||||
return true;
|
||||
}
|
||||
@Override
|
||||
public Class<TileEntityCounter> getTileEntityClass() {
|
||||
return TileEntityCounter.class;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public TileEntityCounter createTileEntity(World world, IBlockState state) {
|
||||
return new TileEntityCounter();
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,36 @@
|
||||
package com.pjht.ssspcore.block.te.counter;
|
||||
|
||||
import net.minecraft.nbt.NBTTagCompound;
|
||||
import net.minecraft.tileentity.TileEntity;
|
||||
|
||||
public class TileEntityCounter extends TileEntity {
|
||||
|
||||
private int count;
|
||||
|
||||
@Override
|
||||
public NBTTagCompound writeToNBT(NBTTagCompound compound) {
|
||||
compound.setInteger("count", count);
|
||||
return super.writeToNBT(compound);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void readFromNBT(NBTTagCompound compound) {
|
||||
count = compound.getInteger("count");
|
||||
super.readFromNBT(compound);
|
||||
}
|
||||
|
||||
public int getCount() {
|
||||
return count;
|
||||
}
|
||||
|
||||
public void incrementCount() {
|
||||
count++;
|
||||
markDirty();
|
||||
}
|
||||
|
||||
public void decrementCount() {
|
||||
count--;
|
||||
markDirty();
|
||||
}
|
||||
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user