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; package lostcave.deathchests.block;
import java.util.ArrayList;
import java.util.Random; 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.BlockContainer;
import net.minecraft.block.material.Material; import net.minecraft.block.material.Material;
import net.minecraft.entity.item.EntityItem;
import net.minecraft.entity.player.EntityPlayer; import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.Item; import net.minecraft.item.Item;
import net.minecraft.item.ItemStack; import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound; import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import net.minecraft.tileentity.TileEntity; import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.ChatComponentText;
import net.minecraft.world.Explosion; import net.minecraft.world.Explosion;
import net.minecraft.world.World; import net.minecraft.world.World;
@ -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_) { 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; if (world.isRemote) return true;
TileDeathChest tiledeathchest = (TileDeathChest) world.getTileEntity(x, y, z); TileDeathChest tileDeathChest = (TileDeathChest) world.getTileEntity(x, y, z);
if (tiledeathchest != null) { if (tileDeathChest != null) {
//TODO: check if appropriate player tileDeathChest.requestItems(player);
//TODO: drop items
return true; return true;
} else return true; } else return true;
} }
/** /**
* Called when a player hits the block. Args: world, x, y, z, player * Called when a player hits the block. Args: world, x, y, z, player
*/ */
@Override @Override
public void onBlockClicked(World p_149699_1_, int p_149699_2_, int p_149699_3_, int p_149699_4_, EntityPlayer p_149699_5_) { public void onBlockClicked(World world, int x, int y, int z, EntityPlayer player) {
//TODO 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(); return new TileDeathChest();
} }
public static class TileDeathChest extends TileEntity { 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 @Override
public void readFromNBT(NBTTagCompound compound) { public void readFromNBT(NBTTagCompound compound) {
Debug.out("Loading death chest from disk");
super.readFromNBT(compound); super.readFromNBT(compound);
containedItems = new ArrayList<>();
//TODO: read contained items 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("--");
} }
@Override @Override
public void writeToNBT(NBTTagCompound compound) { public void writeToNBT(NBTTagCompound compound) {
Debug.out("Writing death chest to disk");
super.writeToNBT(compound); super.writeToNBT(compound);
//TODO: write contained items NBTTagList stacks = new NBTTagList();
}
//TODO: some way of adding items 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: some way of getting the items
} }
//TODO: Custom texture //TODO: Custom texture
//TODO: add chest inventory
//TODO: drop chest contents //TODO: drop chest contents
//TODO: Remove chest when empty //TODO: Remove chest when empty
//TODO: make unable to break by explosion (configurable?) //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 debug = true;
public static boolean fixLog4J = true; public static boolean fixLog4J = true;
public static int maxDistance = 50; 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() { public static void writeConfig() {
String configOut = config_header; 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 += "\n\n# The maximum search radius for finding a suitable location for placing the death chest\n";
configOut += "maxDistance="; configOut += "maxDistance=";
configOut += Integer.toString(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); File configFile = new File(configDir, config_file_name);
try { try {
@ -62,6 +70,14 @@ public class Config {
maxDistance = Integer.parseInt(configData.get(i).substring(equalsSign + 1)); maxDistance = Integer.parseInt(configData.get(i).substring(equalsSign + 1));
break; 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: { default: {
System.err.println("Failed parsing config entry: " + configData.get(i)); System.err.println("Failed parsing config entry: " + configData.get(i));
FMLCommonHandler.instance().exitJava(1, false); FMLCommonHandler.instance().exitJava(1, false);