Add event hook that places the death chest in the world
parent
84beefbbce
commit
a618f200ee
|
@ -52,7 +52,8 @@ public class DeathChests {
|
|||
GameRegistry.registerItem(Obituary.getInstance(), "obituary");
|
||||
Debug.out("Registered block and item.");
|
||||
|
||||
// TODO (probably here): Add death event handler
|
||||
MinecraftForge.EVENT_BUS.register(new EventHook());
|
||||
Debug.out("Registered event hook.");
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
|
|
|
@ -0,0 +1,166 @@
|
|||
package lostcave.deathchests;
|
||||
|
||||
import cpw.mods.fml.common.eventhandler.SubscribeEvent;
|
||||
import lostcave.deathchests.block.BlockDeathChest;
|
||||
import lostcave.deathchests.util.Config;
|
||||
import lostcave.deathchests.util.Debug;
|
||||
import net.minecraft.block.BlockAir;
|
||||
import net.minecraft.entity.player.EntityPlayer;
|
||||
import net.minecraft.world.World;
|
||||
|
||||
public class EventHook {
|
||||
|
||||
@SubscribeEvent
|
||||
public void PlayerDropsEvent(net.minecraftforge.event.entity.player.PlayerDropsEvent event) {
|
||||
// This is kinda misleadingly named.
|
||||
// The event fires only when a player entity produces drops (due to death),
|
||||
// not when a player drops an item.
|
||||
if (!event.entity.worldObj.isRemote) {
|
||||
Debug.out(event.entityPlayer.getDisplayName());
|
||||
// subtract 0.5 because that's the center of the block
|
||||
int x = (int) Math.round(event.entityPlayer.posX - 0.5d);
|
||||
int y = (int) Math.round(event.entityPlayer.posY - 0.5d);
|
||||
int z = (int) Math.round(event.entityPlayer.posZ - 0.5d);
|
||||
Debug.out("X: " + Long.toString(x));
|
||||
Debug.out("Y: " + Long.toString(y));
|
||||
Debug.out("Z: " + Long.toString(z));
|
||||
|
||||
// Don’t place the chest outside the world
|
||||
if (y < 0) y = 0;
|
||||
if (y > 255) y = 255;
|
||||
|
||||
boolean foundSuitableBlock = false;
|
||||
int[] suitableBlock = new int[3];
|
||||
World world = event.entity.worldObj;
|
||||
// search for air block by checking the outside walls of an expanding cube
|
||||
if (world.getBlock(x, y, z) instanceof BlockAir) {
|
||||
suitableBlock[0] = x;
|
||||
suitableBlock[1] = y;
|
||||
suitableBlock[2] = z;
|
||||
foundSuitableBlock = true;
|
||||
} else {
|
||||
for (int i = 1; i <= Config.maxDistance; i++) {
|
||||
int j; //x
|
||||
int k; //y
|
||||
int l; //z
|
||||
// Top and bottom sides
|
||||
if (!foundSuitableBlock) {
|
||||
k = -1 * i;
|
||||
for (j = -1 * i; j <= i; j++) {
|
||||
if (k < 0) break;
|
||||
for (l = -1 * i; l <= i; l++) {
|
||||
if (world.getBlock(x + j, y + k, z + l) instanceof BlockAir) {
|
||||
suitableBlock[0] = x + j;
|
||||
suitableBlock[1] = y + k;
|
||||
suitableBlock[2] = z + l;
|
||||
foundSuitableBlock = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (foundSuitableBlock) break;
|
||||
}
|
||||
}
|
||||
if (!foundSuitableBlock) {
|
||||
k = i;
|
||||
for (j = -1 * i; j <= i; j++) {
|
||||
if (k > 255) break;
|
||||
for (l = -1 * i; l <= i; l++) {
|
||||
if (world.getBlock(x + j, y + k, z + l) instanceof BlockAir) {
|
||||
suitableBlock[0] = x + j;
|
||||
suitableBlock[1] = y + k;
|
||||
suitableBlock[2] = z + l;
|
||||
foundSuitableBlock = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (foundSuitableBlock) break;
|
||||
}
|
||||
}
|
||||
// Left and right sides
|
||||
if (!foundSuitableBlock) {
|
||||
j = -1 * i;
|
||||
for (k = (-1 * i) + 1; k <= i - 1; k++) {
|
||||
if (k < 0 || k > 255) continue;
|
||||
for (l = -1 * i; l <= i; l++) {
|
||||
if (world.getBlock(x + j, y + k, z + l) instanceof BlockAir) {
|
||||
suitableBlock[0] = x + j;
|
||||
suitableBlock[1] = y + k;
|
||||
suitableBlock[2] = z + l;
|
||||
foundSuitableBlock = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (foundSuitableBlock) break;
|
||||
}
|
||||
}
|
||||
if (!foundSuitableBlock) {
|
||||
j = i;
|
||||
for (k = (-1 * i) + 1; k <= i - 1; k++) {
|
||||
if (k < 0 || k > 255) continue;
|
||||
for (l = -1 * i; l <= i; l++) {
|
||||
if (world.getBlock(x + j, y + k, z + l) instanceof BlockAir) {
|
||||
suitableBlock[0] = x + j;
|
||||
suitableBlock[1] = y + k;
|
||||
suitableBlock[2] = z + l;
|
||||
foundSuitableBlock = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (foundSuitableBlock) break;
|
||||
}
|
||||
}
|
||||
// Front and back sides
|
||||
if (!foundSuitableBlock) {
|
||||
l = -1 * i;
|
||||
for (j = (-1 * i) + 1; j <= i - 1; j++) {
|
||||
for (k = (-1 * i) + 1; k <= i - 1; k++) {
|
||||
if (k < 0 || k > 255) continue;
|
||||
if (world.getBlock(x + j, y + k, z + l) instanceof BlockAir) {
|
||||
suitableBlock[0] = x + j;
|
||||
suitableBlock[1] = y + k;
|
||||
suitableBlock[2] = z + l;
|
||||
foundSuitableBlock = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (foundSuitableBlock) break;
|
||||
}
|
||||
}
|
||||
if (!foundSuitableBlock) {
|
||||
l = i;
|
||||
for (j = (-1 * i) + 1; j <= i - 1; j++) {
|
||||
for (k = (-1 * i) + 1; k <= i - 1; k++) {
|
||||
if (k < 0 || k > 255) continue;
|
||||
if (world.getBlock(x + j, y + k, z + l) instanceof BlockAir) {
|
||||
suitableBlock[0] = x + j;
|
||||
suitableBlock[1] = y + k;
|
||||
suitableBlock[2] = z + l;
|
||||
foundSuitableBlock = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (foundSuitableBlock) break;
|
||||
}
|
||||
}
|
||||
|
||||
// big outer loop
|
||||
if (foundSuitableBlock) break;
|
||||
}
|
||||
}
|
||||
if (foundSuitableBlock) {
|
||||
Debug.out("Suitable block X: " + Integer.toString(suitableBlock[0]));
|
||||
Debug.out("Suitable block Y: " + Integer.toString(suitableBlock[1]));
|
||||
Debug.out("Suitable block Z: " + Integer.toString(suitableBlock[2]));
|
||||
//TODO: place death chest
|
||||
world.setBlock(suitableBlock[0], suitableBlock[1], suitableBlock[2], BlockDeathChest.getInstance());
|
||||
//TODO: Get the player's items and put them in the death chest
|
||||
event.setCanceled(true);
|
||||
Debug.out("Canceled PlayerDropEvent");
|
||||
//TODO: Tell the player about death chest location upon respawn
|
||||
} else {
|
||||
Debug.out("No suitable location could be found.");
|
||||
//TODO: tell player that a death chest couldn’t be placed and that the items have been dropped
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue