Compare commits

...

9 Commits

Author SHA1 Message Date
BodgeMaster 0831ad4b2f ItemObituariy: finish implementation 2022-12-20 21:56:44 +01:00
BodgeMaster 1b347a5490 Main: remove useless TODO 2022-12-20 21:54:48 +01:00
BodgeMaster df0f3d2243 BlockDeathChest: Finish up by adding a texture 2022-12-20 21:03:32 +01:00
BodgeMaster aa2ae4f20b Config: Make obituary item configurable 2022-12-20 20:25:00 +01:00
BodgeMaster d3f300da85 EventHook: Add dropped items to the death chest 2022-12-20 20:21:54 +01:00
BodgeMaster 3c320d9bbf BlockDeathChest: Finish TileEntity implementation 2022-12-20 20:21:02 +01:00
BodgeMaster 32610cf937 Main class: Register TileEntity 2022-12-20 20:17:16 +01:00
BodgeMaster 397d66ce54 BlockDeathChest: start implementation of TileEntity 2022-12-20 17:48:18 +01:00
BodgeMaster 140e376ca8 BlockDeathChest: Switch base class from BlockEnderChest to BlockContainer 2022-12-20 17:47:33 +01:00
7 changed files with 212 additions and 54 deletions

View File

@ -1,12 +1,14 @@
package lostcave.deathchests;
import lostcave.deathchests.block.BlockDeathChest;
import lostcave.deathchests.block.BlockDeathChest.TileDeathChest;
import lostcave.deathchests.item.ItemObituary;
import lostcave.deathchests.util.Config;
import lostcave.deathchests.util.DeathChestStorage;
import lostcave.deathchests.util.Debug;
import net.minecraft.client.Minecraft;
import net.minecraft.init.Blocks;
import net.minecraft.tileentity.TileEntity;
import net.minecraftforge.common.DimensionManager;
import net.minecraftforge.common.MinecraftForge;
@ -56,7 +58,9 @@ public class DeathChests {
Debug.out("Received FMLInitializationEvent");
GameRegistry.registerBlock(BlockDeathChest.getInstance(), "death_chest");
GameRegistry.registerItem(ItemObituary.getInstance(), "obituary");
if (Config.giveObituary) {
GameRegistry.registerItem(ItemObituary.getInstance(), "obituary");
}
Debug.out("Registered block and item.");
MinecraftForge.EVENT_BUS.register(new EventHook());
@ -67,12 +71,12 @@ public class DeathChests {
@EventHandler
public void init(FMLPostInitializationEvent event) {
Debug.out("Received FMLPostInitializationEvent");
TileEntity.addMapping(TileDeathChest.class, "death_chest");
}
@EventHandler
public void init(FMLServerAboutToStartEvent event) {
Debug.out("Received FMLServerAboutToStartEvent");
//TODO: get world save location
worldSaveLocation = DimensionManager.getCurrentSaveRootDirectory();
DeathChestStorage.load(worldSaveLocation);
}

View File

@ -1,15 +1,20 @@
package lostcave.deathchests;
import java.util.ArrayList;
import cpw.mods.fml.common.eventhandler.SubscribeEvent;
import cpw.mods.fml.common.gameevent.PlayerEvent;
import lostcave.deathchests.block.BlockDeathChest;
import lostcave.deathchests.block.BlockDeathChest.TileDeathChest;
import lostcave.deathchests.item.ItemObituary;
import lostcave.deathchests.util.Config;
import lostcave.deathchests.util.DeathChestStorage;
import lostcave.deathchests.util.Debug;
import net.minecraft.block.BlockAir;
import net.minecraft.entity.item.EntityItem;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.world.World;
import net.minecraftforge.event.entity.EntityEvent.EntityConstructing;
import net.minecraftforge.event.entity.player.PlayerDropsEvent;
@ -30,6 +35,8 @@ public class EventHook {
Debug.out("X: " + Long.toString(x));
Debug.out("Y: " + Long.toString(y));
Debug.out("Z: " + Long.toString(z));
//TODO: Dont place a death chest for an empty inventory
// Dont place the chest outside the world
if (y < 0) y = 0;
@ -162,12 +169,20 @@ public class EventHook {
suitableBlock[3] = event.entityPlayer.dimension;
world.setBlock(suitableBlock[0], suitableBlock[1], suitableBlock[2], BlockDeathChest.getInstance());
//TODO: Get the player's items and put them in the death chest
TileDeathChest tile = (TileDeathChest) world.getTileEntity(suitableBlock[0], suitableBlock[1], suitableBlock[2]);
tile.owner = event.entityPlayer.getGameProfile().getId();
ArrayList<EntityItem> drops = event.drops;
for (EntityItem drop : drops) {
Debug.out(drop.getEntityItem().getDisplayName());
tile.addItemStack(drop.getEntityItem());
}
String uuid = event.entityPlayer.getGameProfile().getId().toString();
Debug.out(uuid);
DeathChestStorage.addNewDeathChestLocation(uuid, suitableBlock);
//TODO: prevent the items in question from spawning instead of canceling the event
event.setCanceled(true);
Debug.out("Canceled PlayerDropEvent");
} else {
@ -183,7 +198,6 @@ public class EventHook {
String uuid = event.player.getGameProfile().getId().toString();
Debug.out("UUID: " + uuid);
//TODO: check for stored chest location
if (DeathChestStorage.hasNewDeathChestLocation(uuid)) {
int[] chestLocation = DeathChestStorage.popNewDeathChestLocation(uuid);
Debug.out("Chest location X: " + Integer.toString(chestLocation[0]));
@ -191,11 +205,24 @@ public class EventHook {
Debug.out("Chest location Z: " + Integer.toString(chestLocation[2]));
Debug.out("Chest location dimension: " + Integer.toString(chestLocation[3]));
//TODO: NBT data
event.player.inventory.addItemStackToInventory(new ItemStack(ItemObituary.getInstance()));
if (Config.giveObituary) {
ItemStack stack = new ItemStack(ItemObituary.getInstance());
NBTTagCompound compound = new NBTTagCompound();
String location = Integer.toString(chestLocation[0]) + " " + Integer.toString(chestLocation[1]) + " " + Integer.toString(chestLocation[2]);
//TODO: Find the name of the dimension instead
String dimension = Integer.toString(chestLocation[3]);
String message = String.format(Config.locationMessage, location, dimension);
compound.setString("message", message);
stack.setTagCompound(compound);
event.player.inventory.addItemStackToInventory(stack);
}
} else {
Debug.out("No death chest location was stored.");
//TODO: tell player that a death chest couldnt be placed and that the items have been dropped
ItemStack stack = new ItemStack(ItemObituary.getInstance());
NBTTagCompound compound = new NBTTagCompound();
compound.setString("message", Config.dropMessage);
stack.setTagCompound(compound);
event.player.inventory.addItemStackToInventory(stack);
}
}

View File

@ -1,36 +1,71 @@
package lostcave.deathchests.block;
import java.util.ArrayList;
import java.util.Random;
import java.util.UUID;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import net.minecraft.block.Block;
import net.minecraft.block.BlockEnderChest;
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.client.renderer.texture.IIconRegister;
import net.minecraft.entity.item.EntityItem;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.init.Blocks;
import net.minecraft.inventory.InventoryEnderChest;
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.tileentity.TileEntityEnderChest;
import net.minecraft.util.ChatComponentText;
import net.minecraft.util.IIcon;
import net.minecraft.world.Explosion;
import net.minecraft.world.World;
public class BlockDeathChest extends BlockEnderChest {
public class BlockDeathChest extends BlockContainer {
private static final BlockDeathChest instance = new BlockDeathChest();
public static BlockDeathChest getInstance() {
return instance;
}
private IIcon[] icons = new IIcon[6];
private BlockDeathChest() {
super();
super(Material.rock);
this.setBlockName("death_chest");
this.disableStats();
this.setBlockUnbreakable();
// same value as bedrock
this.setResistance(6000000.0F);
this.setBlockBounds(0.0625F, 0.0F, 0.0625F, 0.9375F, 0.875F, 0.9375F);
this.setBlockTextureName("deathchests:deathchest_top");
}
@Override
public void registerBlockIcons(IIconRegister register) {
icons[0] = register.registerIcon("deathchests:deathchest_top");
icons[1] = register.registerIcon("deathchests:deathchest_top");
icons[2] = register.registerIcon("deathchests:deathchest_side");
icons[3] = register.registerIcon("deathchests:deathchest_side");
icons[4] = register.registerIcon("deathchests:deathchest_side");
icons[5] = register.registerIcon("deathchests:deathchest_side");
}
@Override
public IIcon getIcon(int side, int meta) {
return icons[side];
}
@Override
public boolean isOpaqueCube() {
return false;
}
@Override
public boolean renderAsNormalBlock() {
return true;
}
@Override
@ -55,21 +90,24 @@ public class BlockDeathChest extends BlockEnderChest {
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);
}
}
/**
@ -81,35 +119,57 @@ public class BlockDeathChest extends BlockEnderChest {
return new TileDeathChest();
}
/**
* A randomly called display update to be able to add particles or other items
* for display
* <br />
* <br/>
* This is here to disable the particles that would otherwise be emitted
*/
@Override
@SideOnly(Side.CLIENT)
public void randomDisplayTick(World p_149734_1_, int p_149734_2_, int p_149734_3_, int p_149734_4_, Random p_149734_5_) {
}
@Override
@SideOnly(Side.CLIENT)
public void registerBlockIcons(IIconRegister p_149651_1_) {
this.blockIcon = p_149651_1_.registerIcon("cobblestone");
}
public static class TileDeathChest extends TileEntity {
//TODO
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);
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("--");
}
@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: Custom texture? (optional)
//TODO: add chest inventory
//TODO: drop chest contents
//TODO: Remove chest when empty
//TODO: make unable to break by explosion (configurable?)
//TODO: Make it so items can only be taken out of the chest?
// Custom GUI? Maybe remove GUI altogether and have it drop the items when broken?
}

View File

@ -1,6 +1,14 @@
package lostcave.deathchests.item;
import java.util.List;
import lostcave.deathchests.util.Config;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.ChatComponentText;
import net.minecraft.world.World;
public class ItemObituary extends Item {
@ -13,9 +21,28 @@ public class ItemObituary extends Item {
private ItemObituary() {
this.setUnlocalizedName("obituary");
this.setTextureName("paper");
this.setMaxStackSize(1);
}
@Override
public ItemStack onItemRightClick(ItemStack stack, World world, EntityPlayer player) {
if (world.isRemote) return stack;
String chatMessage = "§4Error: Missing data";
if (stack.hasTagCompound()) {
NBTTagCompound compound = stack.getTagCompound();
if (compound.hasKey("message")) {
chatMessage = compound.getString("message");
}
}
player.addChatMessage(new ChatComponentText(chatMessage));
return stack;
}
//TODO: NBT data
//TODO: right click action
//TODO: item description
@Override
public void addInformation(ItemStack stack, EntityPlayer player, List lores, boolean bool) {
lores.add("Right-click me!");
}
}

View File

@ -15,8 +15,13 @@ 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.";
public static boolean giveObituary = true;
public static String dropMessage = "§4A death chest could not be placed for this death. Your items have been dropped.";
public static String locationMessage = "§eThe death chest for this death is at %s in dimension %s.";
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 +34,21 @@ 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;
configOut += "\n\n# Whether to give respawning players an obituary item\n";
configOut += "giveObituary=";
configOut += giveObituary ? "true" : "false";
configOut += "\n\n# The message to display when no suitable location for a death chest could be found and the items have been dropped";
configOut += "dropMessage=";
configOut += dropMessage;
configOut += "\n\n# The message to display when no suitable location for a death chest could be found and the items have been dropped";
configOut += "locationMessage=";
configOut += locationMessage;
File configFile = new File(configDir, config_file_name);
try {
@ -62,6 +82,26 @@ 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;
}
case ("giveObituary"): {
giveObituary = configData.get(i).substring(equalsSign + 1).equals("true") ? true : false;
break;
}
case ("dropMessage"): {
dropMessage = configData.get(i).substring(equalsSign+1);
break;
}
case ("locationMessage"): {
locationMessage = configData.get(i).substring(equalsSign+1);
break;
}
default: {
System.err.println("Failed parsing config entry: " + configData.get(i));
FMLCommonHandler.instance().exitJava(1, false);

Binary file not shown.

After

Width:  |  Height:  |  Size: 757 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 753 B