Files
CustomNPC-Plus/src/main/java/noppes/npcs/blocks/BlockLightable.java
T

71 lines
2.1 KiB
Java
Raw Normal View History

2018-09-13 12:03:28 -04:00
package noppes.npcs.blocks;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import net.minecraft.block.Block;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
2025-02-04 02:19:51 -05:00
import net.minecraft.nbt.NBTTagCompound;
2018-09-13 12:03:28 -04:00
import net.minecraft.tileentity.TileEntity;
import net.minecraft.world.World;
2023-01-12 21:33:06 -05:00
import java.util.Random;
2025-02-04 02:19:51 -05:00
public abstract class BlockLightable extends BlockRotated {
2018-09-13 12:03:28 -04:00
protected BlockLightable(Block block, boolean lit) {
super(block);
2025-02-04 02:19:51 -05:00
2018-09-13 12:03:28 -04:00
if (lit)
this.setLightLevel(1.0F);
}
public abstract Block unlitBlock();
public abstract Block litBlock();
2025-02-04 02:19:51 -05:00
2018-09-13 12:03:28 -04:00
@Override
public boolean onBlockActivated(World world, int x, int y, int z, EntityPlayer player, int par6, float par7, float par8, float par9){
2025-02-04 02:19:51 -05:00
// Get the current tile entity and store its NBT
TileEntity oldTile = world.getTileEntity(x, y, z);
NBTTagCompound compound = new NBTTagCompound();
oldTile.writeToNBT(compound);
// Determine which block to set (lit/unlit) based on the current one
Block newBlock = (litBlock() == this ? unlitBlock() : litBlock());
int meta = world.getBlockMetadata(x, y, z);
// Change the block; this creates a new tile entity instance
world.setBlock(x, y, z, newBlock, meta, 2);
// Get the new tile and load the stored NBT
TileEntity newTile = world.getTileEntity(x, y, z);
if(newTile != null){
newTile.readFromNBT(compound);
newTile.markDirty();
}
world.markBlockForUpdate(x, y, z);
world.notifyBlockOfNeighborChange(x, y, z, newBlock);
2018-09-13 12:03:28 -04:00
return true;
}
@Override
public Item getItemDropped(int p_149650_1_, Random p_149650_2_, int p_149650_3_){
return Item.getItemFromBlock(litBlock());
}
@Override
@SideOnly(Side.CLIENT)
public Item getItem(World p_149694_1_, int p_149694_2_, int p_149694_3_, int p_149694_4_){
return Item.getItemFromBlock(litBlock());
}
@Override
protected ItemStack createStackedBlock(int p_149644_1_){
return new ItemStack(litBlock());
}
}