2013-12-17 21:48:21 +01:00
|
|
|
package com.pahimar.ee3.handler;
|
2013-08-23 16:59:50 +02:00
|
|
|
|
|
|
|
import com.pahimar.ee3.configuration.ConfigurationSettings;
|
|
|
|
import com.pahimar.ee3.event.ActionEvent;
|
|
|
|
import com.pahimar.ee3.event.ActionEvent.ActionResult;
|
|
|
|
import com.pahimar.ee3.event.ActionRequestEvent;
|
|
|
|
import com.pahimar.ee3.event.WorldTransmutationEvent;
|
2013-12-17 21:48:21 +01:00
|
|
|
import com.pahimar.ee3.helper.TransmutationHelper;
|
2013-08-23 16:59:50 +02:00
|
|
|
import com.pahimar.ee3.lib.ActionTypes;
|
|
|
|
import com.pahimar.ee3.lib.ItemUpdateTypes;
|
|
|
|
import com.pahimar.ee3.lib.Particles;
|
|
|
|
import com.pahimar.ee3.lib.Sounds;
|
|
|
|
import com.pahimar.ee3.network.PacketTypeHandler;
|
|
|
|
import com.pahimar.ee3.network.packet.PacketItemUpdate;
|
|
|
|
import com.pahimar.ee3.network.packet.PacketSoundEvent;
|
|
|
|
import com.pahimar.ee3.network.packet.PacketSpawnParticle;
|
|
|
|
import cpw.mods.fml.common.network.PacketDispatcher;
|
|
|
|
import cpw.mods.fml.common.network.Player;
|
2013-12-16 02:26:42 +01:00
|
|
|
import net.minecraft.block.Block;
|
|
|
|
import net.minecraft.entity.player.EntityPlayer;
|
|
|
|
import net.minecraft.item.ItemStack;
|
|
|
|
import net.minecraftforge.common.ForgeDirection;
|
|
|
|
import net.minecraftforge.common.MinecraftForge;
|
|
|
|
import net.minecraftforge.event.Event.Result;
|
|
|
|
import net.minecraftforge.event.ForgeSubscribe;
|
2013-08-23 16:59:50 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Equivalent-Exchange-3
|
2013-12-16 02:26:42 +01:00
|
|
|
* <p/>
|
2013-08-23 16:59:50 +02:00
|
|
|
* WorldTransmutationHandler
|
2013-12-16 02:26:42 +01:00
|
|
|
*
|
2013-08-23 16:59:50 +02:00
|
|
|
* @author pahimar
|
|
|
|
*/
|
2013-12-16 02:26:42 +01:00
|
|
|
public class WorldTransmutationHandler
|
|
|
|
{
|
2013-08-23 16:59:50 +02:00
|
|
|
|
2013-12-16 02:26:42 +01:00
|
|
|
public static void handleWorldTransmutation(EntityPlayer thePlayer, int originX, int originY, int originZ, byte rangeX, byte rangeY, byte rangeZ, byte sideHit, String data)
|
|
|
|
{
|
2013-08-23 16:59:50 +02:00
|
|
|
|
|
|
|
ActionRequestEvent actionRequestEvent = null;
|
|
|
|
ActionEvent actionEvent = null;
|
|
|
|
|
|
|
|
int lowerBoundX = -1 * rangeX / 2;
|
|
|
|
int upperBoundX = -1 * lowerBoundX;
|
|
|
|
int lowerBoundY = -1 * rangeY / 2;
|
|
|
|
int upperBoundY = -1 * lowerBoundY;
|
|
|
|
int lowerBoundZ = -1 * rangeZ / 2;
|
|
|
|
int upperBoundZ = -1 * lowerBoundZ;
|
|
|
|
boolean anySuccess = false;
|
|
|
|
|
|
|
|
double xShift = 0;
|
|
|
|
double yShift = 0;
|
|
|
|
double zShift = 0;
|
|
|
|
|
|
|
|
int xSign = 1;
|
|
|
|
int ySign = 1;
|
|
|
|
int zSign = 1;
|
|
|
|
|
2013-12-16 02:26:42 +01:00
|
|
|
switch (ForgeDirection.getOrientation(sideHit))
|
|
|
|
{
|
|
|
|
case UP:
|
|
|
|
{
|
2013-08-23 16:59:50 +02:00
|
|
|
yShift = 1.5D;
|
|
|
|
break;
|
|
|
|
}
|
2013-12-16 02:26:42 +01:00
|
|
|
case DOWN:
|
|
|
|
{
|
2013-08-23 16:59:50 +02:00
|
|
|
yShift = 0.1D;
|
|
|
|
ySign = -1;
|
|
|
|
break;
|
|
|
|
}
|
2013-12-16 02:26:42 +01:00
|
|
|
case NORTH:
|
|
|
|
{
|
2013-08-23 16:59:50 +02:00
|
|
|
zShift = 1D;
|
|
|
|
zSign = -1;
|
|
|
|
break;
|
|
|
|
}
|
2013-12-16 02:26:42 +01:00
|
|
|
case SOUTH:
|
|
|
|
{
|
2013-08-23 16:59:50 +02:00
|
|
|
zShift = 1D;
|
|
|
|
break;
|
|
|
|
}
|
2013-12-16 02:26:42 +01:00
|
|
|
case EAST:
|
|
|
|
{
|
2013-08-23 16:59:50 +02:00
|
|
|
xShift = 1D;
|
|
|
|
break;
|
|
|
|
}
|
2013-12-16 02:26:42 +01:00
|
|
|
case WEST:
|
|
|
|
{
|
2013-08-23 16:59:50 +02:00
|
|
|
xShift = 1D;
|
|
|
|
xSign = -1;
|
|
|
|
break;
|
|
|
|
}
|
2013-12-16 02:26:42 +01:00
|
|
|
case UNKNOWN:
|
|
|
|
{
|
2013-08-23 16:59:50 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2013-12-16 02:26:42 +01:00
|
|
|
for (int x = lowerBoundX; x <= upperBoundX; x++)
|
|
|
|
{
|
|
|
|
for (int y = lowerBoundY; y <= upperBoundY; y++)
|
|
|
|
{
|
|
|
|
for (int z = lowerBoundZ; z <= upperBoundZ; z++)
|
|
|
|
{
|
2013-08-23 16:59:50 +02:00
|
|
|
|
|
|
|
actionEvent = new WorldTransmutationEvent(ActionTypes.TRANSMUTATION, thePlayer.getCurrentEquippedItem(), thePlayer, thePlayer.worldObj, originX + x, originY + y, originZ + z, false, data);
|
|
|
|
|
2013-12-16 02:26:42 +01:00
|
|
|
if (actionEvent != null)
|
|
|
|
{
|
2013-08-23 16:59:50 +02:00
|
|
|
actionRequestEvent = new ActionRequestEvent(thePlayer, actionEvent, originX + x, originY + y, originZ + z, sideHit);
|
|
|
|
MinecraftForge.EVENT_BUS.post(actionRequestEvent);
|
|
|
|
|
2013-12-16 02:26:42 +01:00
|
|
|
if (actionRequestEvent.allowEvent != Result.DENY)
|
|
|
|
{
|
2013-08-23 16:59:50 +02:00
|
|
|
MinecraftForge.EVENT_BUS.post(actionEvent);
|
|
|
|
}
|
|
|
|
|
2013-12-16 02:26:42 +01:00
|
|
|
if (actionEvent.actionResult == ActionResult.SUCCESS)
|
|
|
|
{
|
|
|
|
if (!anySuccess)
|
|
|
|
{
|
2013-08-23 16:59:50 +02:00
|
|
|
anySuccess = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
PacketDispatcher.sendPacketToAllAround(originX + x, originY + y, originZ + z, 64D, thePlayer.worldObj.provider.dimensionId, PacketTypeHandler.populatePacket(new PacketSpawnParticle(Particles.LARGE_SMOKE, originX + x + xShift * xSign, originY + y + yShift * ySign, originZ + z + zShift * zSign, 0D * xSign, 0.05D * ySign, 0D * zSign)));
|
|
|
|
PacketDispatcher.sendPacketToAllAround(originX + x, originY + y, originZ + z, 64D, thePlayer.worldObj.provider.dimensionId, PacketTypeHandler.populatePacket(new PacketSpawnParticle(Particles.LARGE_EXPLODE, originX + x + xShift * xSign, originY + y + yShift * ySign, originZ + z + zShift * zSign, 0D * xSign, 0.15D * ySign, 0D * zSign)));
|
|
|
|
}
|
2013-12-16 02:26:42 +01:00
|
|
|
else if (actionEvent.actionResult == ActionResult.FAILURE)
|
|
|
|
{
|
|
|
|
if (!(actionEvent.world.getBlockId(originX + x, originY + y, originZ + z) == 0))
|
|
|
|
{
|
2013-08-23 16:59:50 +02:00
|
|
|
// TODO Fancy particle motion
|
|
|
|
PacketDispatcher.sendPacketToAllAround(originX + x, originY + y, originZ + z, 64D, thePlayer.worldObj.provider.dimensionId, PacketTypeHandler.populatePacket(new PacketSpawnParticle(Particles.RED_DUST, originX + x + xShift * xSign, originY + y + yShift * ySign, originZ + z + zShift * zSign, 0D * xSign, 0.05D * ySign, 0D * zSign)));
|
|
|
|
PacketDispatcher.sendPacketToAllAround(originX + x, originY + y, originZ + z, 64D, thePlayer.worldObj.provider.dimensionId, PacketTypeHandler.populatePacket(new PacketSpawnParticle(Particles.WITCH_MAGIC, originX + x + xShift * xSign, originY + y + yShift * ySign, originZ + z + zShift * zSign, 0D * xSign, 0.05D * ySign, 0D * zSign)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-16 02:26:42 +01:00
|
|
|
if (anySuccess)
|
|
|
|
{
|
2013-08-23 16:59:50 +02:00
|
|
|
PacketDispatcher.sendPacketToAllAround(originX, originY, originZ, 64D, thePlayer.worldObj.provider.dimensionId, PacketTypeHandler.populatePacket(new PacketSoundEvent(thePlayer.username, Sounds.TRANSMUTE, originX, originY, originZ, 0.5F, 1.0F)));
|
|
|
|
}
|
2013-12-16 02:26:42 +01:00
|
|
|
else
|
|
|
|
{
|
2013-08-23 16:59:50 +02:00
|
|
|
PacketDispatcher.sendPacketToAllAround(originX, originY, originZ, 64D, thePlayer.worldObj.provider.dimensionId, PacketTypeHandler.populatePacket(new PacketSoundEvent(thePlayer.username, Sounds.FAIL, originX, originY, originZ, 1.5F, 1.5F)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@ForgeSubscribe
|
2013-12-16 02:26:42 +01:00
|
|
|
public void onWorldTransmutationEvent(WorldTransmutationEvent event)
|
|
|
|
{
|
2013-08-23 16:59:50 +02:00
|
|
|
|
|
|
|
int id = event.world.getBlockId(event.x, event.y, event.z);
|
|
|
|
int meta = event.world.getBlockMetadata(event.x, event.y, event.z);
|
|
|
|
boolean result = false;
|
|
|
|
|
|
|
|
Block currentBlock = Block.blocksList[id];
|
|
|
|
|
2013-12-16 02:26:42 +01:00
|
|
|
if (currentBlock != null)
|
|
|
|
{
|
2013-08-23 16:59:50 +02:00
|
|
|
meta = currentBlock.damageDropped(meta);
|
|
|
|
}
|
|
|
|
|
|
|
|
ItemStack worldStack = new ItemStack(id, 1, meta);
|
|
|
|
ItemStack targetStack = new ItemStack(event.targetID, 1, event.targetMeta);
|
|
|
|
|
2013-12-16 02:26:42 +01:00
|
|
|
if (!worldStack.isItemEqual(targetStack))
|
|
|
|
{
|
|
|
|
if (EquivalencyHandler.instance().areWorldEquivalent(worldStack, targetStack))
|
|
|
|
{
|
|
|
|
if (event.itemStack != null)
|
|
|
|
{
|
|
|
|
if (event.itemStack.getItemDamage() <= event.itemStack.getMaxDamage())
|
|
|
|
{
|
2013-08-23 16:59:50 +02:00
|
|
|
result = TransmutationHelper.transmuteInWorld(event.world, event.player, event.player.getCurrentEquippedItem(), event.x, event.y, event.z, event.targetID, event.targetMeta);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-16 02:26:42 +01:00
|
|
|
if (result)
|
|
|
|
{
|
2013-08-23 16:59:50 +02:00
|
|
|
event.actionResult = ActionResult.SUCCESS;
|
|
|
|
|
|
|
|
int currentSlot = event.player.inventory.currentItem;
|
|
|
|
event.itemStack.damageItem(ConfigurationSettings.TRANSMUTE_COST_BLOCK, event.player);
|
|
|
|
|
2013-12-16 02:26:42 +01:00
|
|
|
if (event.itemStack.stackSize < 1)
|
|
|
|
{
|
2013-08-23 16:59:50 +02:00
|
|
|
event.player.inventory.setInventorySlotContents(currentSlot, null);
|
|
|
|
PacketDispatcher.sendPacketToPlayer(PacketTypeHandler.populatePacket(new PacketItemUpdate((byte) currentSlot, ItemUpdateTypes.DESTROYED)), (Player) event.player);
|
|
|
|
event.player.worldObj.playSoundAtEntity(event.player, "random.break", 0.8F, 0.8F + event.player.worldObj.rand.nextFloat() * 0.4F);
|
|
|
|
}
|
|
|
|
}
|
2013-12-16 02:26:42 +01:00
|
|
|
else
|
|
|
|
{
|
2013-08-23 16:59:50 +02:00
|
|
|
event.actionResult = ActionResult.FAILURE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|