203 lines
9.4 KiB
Java
203 lines
9.4 KiB
Java
package lostcave.deathchests;
|
||
|
||
import cpw.mods.fml.common.eventhandler.SubscribeEvent;
|
||
import cpw.mods.fml.common.gameevent.PlayerEvent;
|
||
import lostcave.deathchests.block.BlockDeathChest;
|
||
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.player.EntityPlayer;
|
||
import net.minecraft.item.ItemStack;
|
||
import net.minecraft.world.World;
|
||
import net.minecraftforge.event.entity.EntityEvent.EntityConstructing;
|
||
import net.minecraftforge.event.entity.player.PlayerDropsEvent;
|
||
|
||
public class EventHook {
|
||
|
||
@SubscribeEvent
|
||
public void death(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;
|
||
|
||
World world = event.entity.worldObj;
|
||
boolean foundSuitableBlock = false;
|
||
int[] suitableBlock = new int[4]; // x y z dimension
|
||
// 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]));
|
||
|
||
Debug.out("Dimension: " + Integer.toString(event.entityPlayer.dimension));
|
||
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
|
||
|
||
String uuid = event.entityPlayer.getGameProfile().getId().toString();
|
||
Debug.out(uuid);
|
||
DeathChestStorage.addNewDeathChestLocation(uuid, suitableBlock);
|
||
|
||
event.setCanceled(true);
|
||
Debug.out("Canceled PlayerDropEvent");
|
||
} else {
|
||
Debug.out("No suitable location could be found.");
|
||
}
|
||
}
|
||
}
|
||
|
||
@SubscribeEvent
|
||
public void respawn(PlayerEvent.PlayerRespawnEvent event) {
|
||
Debug.out(event.player.getDisplayName() + " respawned");
|
||
|
||
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]));
|
||
Debug.out("Chest location Y: " + Integer.toString(chestLocation[1]));
|
||
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()));
|
||
} else {
|
||
Debug.out("No death chest location was stored.");
|
||
//TODO: tell player that a death chest couldn’t be placed and that the items have been dropped
|
||
}
|
||
}
|
||
|
||
}
|