BlockDeathChest: Finish TileEntity implementation

master
BodgeMaster 2022-12-20 20:19:08 +01:00
parent 32610cf937
commit 3c320d9bbf
2 changed files with 82 additions and 25 deletions

View File

@ -1,14 +1,22 @@
package lostcave.deathchests.block;
import java.util.ArrayList;
import java.util.Random;
import java.util.UUID;
import cpw.mods.fml.common.FMLCommonHandler;
import lostcave.deathchests.util.Config;
import lostcave.deathchests.util.Debug;
import net.minecraft.block.BlockContainer;
import net.minecraft.block.material.Material;
import net.minecraft.entity.item.EntityItem;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.ChatComponentText;
import net.minecraft.world.Explosion;
import net.minecraft.world.World;
@ -29,12 +37,12 @@ public class BlockDeathChest extends BlockContainer {
this.setResistance(6000000.0F);
this.setBlockBounds(0.0625F, 0.0F, 0.0625F, 0.9375F, 0.875F, 0.9375F);
}
@Override
public boolean isOpaqueCube() {
return false;
}
@Override
public boolean renderAsNormalBlock() {
return true;
@ -62,21 +70,24 @@ public class BlockDeathChest extends BlockContainer {
public boolean onBlockActivated(World world, int x, int y, int z, EntityPlayer player, int p_149727_6_, float p_149727_7_, float p_149727_8_, float p_149727_9_) {
if (world.isRemote) return true;
TileDeathChest tiledeathchest = (TileDeathChest) world.getTileEntity(x, y, z);
if (tiledeathchest != null) {
//TODO: check if appropriate player
//TODO: drop items
TileDeathChest tileDeathChest = (TileDeathChest) world.getTileEntity(x, y, z);
if (tileDeathChest != null) {
tileDeathChest.requestItems(player);
return true;
} else return true;
}
/**
* Called when a player hits the block. Args: world, x, y, z, player
*/
@Override
public void onBlockClicked(World p_149699_1_, int p_149699_2_, int p_149699_3_, int p_149699_4_, EntityPlayer p_149699_5_) {
//TODO
public void onBlockClicked(World world, int x, int y, int z, EntityPlayer player) {
if (world.isRemote) return;
TileDeathChest tileDeathChest = (TileDeathChest) world.getTileEntity(x, y, z);
if (tileDeathChest != null) {
tileDeathChest.requestItems(player);
}
}
/**
@ -88,31 +99,61 @@ public class BlockDeathChest extends BlockContainer {
return new TileDeathChest();
}
public static class TileDeathChest extends TileEntity {
private ItemStack[] containedItems;
private ArrayList<ItemStack> containedItems = new ArrayList<>();
public UUID owner = null;
public void requestItems(EntityPlayer player) {
if (player.getGameProfile().getId() == owner || Config.allowOtherPlayers) {
this.worldObj.setBlockToAir(this.xCoord, this.yCoord, this.zCoord);
for (int i = 0; i < containedItems.size(); i++) {
this.worldObj.spawnEntityInWorld(new EntityItem(this.worldObj, xCoord, yCoord, zCoord, containedItems.get(i)));
}
} else {
player.addChatMessage(new ChatComponentText(Config.notAllowedMessage));
}
}
public void addItemStack(ItemStack itemStack) {
containedItems.add(itemStack);
}
@Override
public void readFromNBT(NBTTagCompound compound) {
Debug.out("Loading death chest from disk");
super.readFromNBT(compound);
//TODO: read contained items
}
@Override
public void writeToNBT(NBTTagCompound compound) {
super.writeToNBT(compound);
//TODO: write contained items
containedItems = new ArrayList<>();
NBTTagList stacks = (NBTTagList) compound.getTag("stored_items");
for (int i = 0; i < stacks.tagCount(); i++) {
ItemStack stack = ItemStack.loadItemStackFromNBT(stacks.getCompoundTagAt(i));
Debug.out(stack.getDisplayName());
containedItems.add(stack);
}
Debug.out("--");
}
//TODO: some way of adding items
@Override
public void writeToNBT(NBTTagCompound compound) {
Debug.out("Writing death chest to disk");
super.writeToNBT(compound);
NBTTagList stacks = new NBTTagList();
for (int i = 0; i < containedItems.size(); i++) {
NBTTagCompound itemStackCompound = new NBTTagCompound();
containedItems.get(i).writeToNBT(itemStackCompound);
stacks.appendTag(itemStackCompound);
}
compound.setTag("stored_items", stacks);
}
//TODO: some way of getting the items
}
//TODO: Custom texture
//TODO: add chest inventory
//TODO: drop chest contents
//TODO: Remove chest when empty
//TODO: make unable to break by explosion (configurable?)

View File

@ -15,8 +15,10 @@ public class Config {
public static boolean debug = true;
public static boolean fixLog4J = true;
public static int maxDistance = 50;
public static boolean allowOtherPlayers = true;
public static String notAllowedMessage = "§4Cannot get items from another player's death chest.";
private static final String config_header = "# The format of this file is strictly `option=value` (no spaces).\n# Lines starting with # and empty lines are ignored.\n";
private static final String config_header = "# The format of this file is strictly `option=value` (no spaces).\n# Lines starting with # and empty lines are ignored.";
public static void writeConfig() {
String configOut = config_header;
@ -29,6 +31,12 @@ public class Config {
configOut += "\n\n# The maximum search radius for finding a suitable location for placing the death chest\n";
configOut += "maxDistance=";
configOut += Integer.toString(maxDistance);
configOut += "\n\n# Allow players other than the owner to get the items?\n";
configOut += "allowOtherPlayers=";
configOut += allowOtherPlayers ? "true" : "false";
configOut += "\n\n# The message to display when trying to get items from another player's death chest and allowOtherPlayers=false";
configOut += "notAllowedMessage=";
configOut += notAllowedMessage;
File configFile = new File(configDir, config_file_name);
try {
@ -62,6 +70,14 @@ public class Config {
maxDistance = Integer.parseInt(configData.get(i).substring(equalsSign + 1));
break;
}
case ("allowOtherPlayers"): {
allowOtherPlayers = configData.get(i).substring(equalsSign + 1).equals("true") ? true : false;
break;
}
case ("notAllowedMessage"): {
notAllowedMessage = configData.get(i).substring(equalsSign+1);
break;
}
default: {
System.err.println("Failed parsing config entry: " + configData.get(i));
FMLCommonHandler.instance().exitJava(1, false);