Skip to content

Instantly share code, notes, and snippets.

@Selim042
Created June 27, 2016 22:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Selim042/10a8d1796ceed7bc21e73a50a2d6a6b9 to your computer and use it in GitHub Desktop.
Save Selim042/10a8d1796ceed7bc21e73a50a2d6a6b9 to your computer and use it in GitHub Desktop.
package selim.randomStuff.tools;
import java.util.List;
import net.minecraft.block.Block;
import net.minecraft.block.state.IBlockState;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.init.Blocks;
import net.minecraft.init.SoundEvents;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.tileentity.TileEntityFurnace;
import net.minecraft.util.EnumActionResult;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.EnumHand;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.SoundCategory;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.text.TextComponentString;
import net.minecraft.util.text.translation.I18n;
import net.minecraft.world.World;
import net.minecraftforge.fml.common.registry.ForgeRegistries;
import net.minecraftforge.fml.common.registry.GameRegistry;
import selim.randomStuff.SelimRandomStuff;
public class BlockShifter extends Item {
private final String name = "blockShifter";
public BlockShifter() {
setUnlocalizedName(name);
this.setRegistryName(name);
setCreativeTab(SelimRandomStuff.randomThingsTab);
setMaxStackSize(1);
}
public String getName() {
return name;
}
public void addInformation(ItemStack itemStack, EntityPlayer player, List list, boolean bool) {
NBTTagCompound nbt = itemStack.getTagCompound();
if (nbt != null) {
String blockRegistry = nbt.getString("blockRegistry");
if (blockRegistry.equals("")) {
list.add("Empty.");
}
else {
list.add("Contains a block.");
//list.add("Contains a §6" + blockRegistry + "§r§7.");
//list.add("Contains a §6" + I18n.translateToLocal(Block.getBlockById(nbt.getInteger("blockID")).getUnlocalizedName()) + "§r§7.");
}
}
else {
list.add("Empty.");
}
}
public boolean hasEffect() {
return true;
}
@Override
public EnumActionResult onItemUse(ItemStack itemStack, EntityPlayer player, World world,
BlockPos pos, EnumHand hand, EnumFacing facing, float hitX, float hitY, float hitZ) {
NBTTagCompound nbt = itemStack.getTagCompound();
if (nbt == null) {
itemStack.setTagCompound(new NBTTagCompound());
nbt = itemStack.getTagCompound();
}
if (nbt.getString("blockRegistry").equals("")) { // picking up block
if (world.canMineBlockBody(player, pos)) {
Block selectedBlock = world.getBlockState(pos).getBlock();
if (!selectedBlock.equals(Blocks.BEDROCK)) {
IBlockState block = world.getBlockState(pos);
nbt.setString("blockRegistry", ForgeRegistries.BLOCKS.getKey(selectedBlock).toString());
nbt.setInteger("blockMeta", Block.getStateId(block));
TileEntity tileEntity = world.getTileEntity(pos);
if (tileEntity != null) {
NBTTagCompound entityData = new NBTTagCompound();
tileEntity.writeToNBT(entityData);
nbt.setTag("blockData", entityData);
world.removeTileEntity(pos);
}
itemStack.setTagCompound(nbt);
if (!world.isRemote) {
world.setBlockToAir(pos);
}
//world.notifyNeighborsOfStateChange(pos, null);
//world.markAndNotifyBlock(pos, world.getChunkFromBlockCoords(pos), world.getBlockState(pos), world.getBlockState(pos), 0);
world.playSound(player, pos, SoundEvents.ENTITY_ENDERMEN_TELEPORT, SoundCategory.PLAYERS, 1.0F, 1.0F);
}
}
}
else { // placing block
if (!world.getBlockState(pos).getBlock().isReplaceable(world, pos) || player.isSneaking()) {
switch (facing) {// determine where the new block goes
case DOWN:
pos = pos.add(0,-1,0);
break;
case EAST:
pos = pos.add(1,0,0);
break;
case NORTH:
pos = pos.add(0,0,-1);
break;
case SOUTH:
pos = pos.add(0,0,1);
break;
case UP:
pos = pos.add(0,1,0);
break;
case WEST:
pos = pos.add(-1,0,0);
break;
default:
break;
}
}
if (world.isAirBlock(pos) || world.getBlockState(pos).getBlock().isReplaceable(world, pos)) { // if the block in the new location is air, or is replaceable, continue
//IBlockState newBlockState = Block.getBlockById(nbt.getInteger("blockID")).getStateById(nbt.getInteger("blockMeta"));
IBlockState newBlockState = ForgeRegistries.BLOCKS.getValue(new ResourceLocation(nbt.getString("blockRegistry"))).getStateById(nbt.getInteger("blockMeta"));
world.setBlockState(pos, newBlockState, 3);
world.playSound(player, pos, SoundEvents.ENTITY_ENDERMEN_TELEPORT, SoundCategory.PLAYERS, 1.0F, 1.0F);
nbt.setString("blockRegistry", "");
TileEntity tileEntity = world.getTileEntity(pos);
if (tileEntity != null && nbt.hasKey("blockData")) {
NBTTagCompound entityData = nbt.getCompoundTag("blockData");
// entityData.setInteger("x", pos.getX());
// entityData.setInteger("y", pos.getY());
// entityData.setInteger("z", pos.getZ());
tileEntity.readFromNBT(entityData);
nbt.removeTag("blockData");
}
itemStack.setTagCompound(nbt);
world.notifyNeighborsOfStateChange(pos, null);
world.markAndNotifyBlock(pos, world.getChunkFromBlockCoords(pos), world.getBlockState(pos), world.getBlockState(pos), 0);
}
}
player.swingArm(hand);
return null;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment