2015-10-05 18:39:45 +02:00
|
|
|
package techreborn.partSystem.parts;
|
|
|
|
|
2015-10-23 17:04:56 +02:00
|
|
|
import codechicken.lib.packet.PacketCustom;
|
|
|
|
import codechicken.lib.raytracer.RayTracer;
|
2015-10-05 18:39:45 +02:00
|
|
|
import codechicken.lib.vec.BlockCoord;
|
2015-10-23 17:04:56 +02:00
|
|
|
import codechicken.lib.vec.Vector3;
|
2015-10-05 18:39:45 +02:00
|
|
|
import codechicken.multipart.MultiPartRegistry;
|
|
|
|
import codechicken.multipart.TMultiPart;
|
2015-10-23 17:04:56 +02:00
|
|
|
import codechicken.multipart.TileMultipart;
|
|
|
|
import cpw.mods.fml.common.eventhandler.EventPriority;
|
|
|
|
import cpw.mods.fml.common.eventhandler.SubscribeEvent;
|
2015-10-05 18:39:45 +02:00
|
|
|
import ic2.api.item.IC2Items;
|
|
|
|
import ic2.core.block.wiring.BlockCable;
|
|
|
|
import ic2.core.block.wiring.TileEntityCable;
|
|
|
|
import net.minecraft.block.Block;
|
2015-10-23 17:04:56 +02:00
|
|
|
import net.minecraft.block.BlockFence;
|
|
|
|
import net.minecraft.entity.player.EntityPlayer;
|
|
|
|
import net.minecraft.init.Blocks;
|
|
|
|
import net.minecraft.item.Item;
|
|
|
|
import net.minecraft.item.ItemStack;
|
|
|
|
import net.minecraft.network.play.client.C08PacketPlayerBlockPlacement;
|
2015-10-05 18:39:45 +02:00
|
|
|
import net.minecraft.tileentity.TileEntity;
|
2015-10-23 17:04:56 +02:00
|
|
|
import net.minecraft.util.MovingObjectPosition;
|
2015-10-05 18:39:45 +02:00
|
|
|
import net.minecraft.world.World;
|
2015-10-23 17:04:56 +02:00
|
|
|
import net.minecraftforge.common.MinecraftForge;
|
|
|
|
import net.minecraftforge.common.util.ForgeDirection;
|
|
|
|
import net.minecraftforge.event.entity.player.PlayerDestroyItemEvent;
|
|
|
|
import net.minecraftforge.event.entity.player.PlayerInteractEvent;
|
2015-10-05 18:39:45 +02:00
|
|
|
import techreborn.partSystem.fmp.FMPModPart;
|
|
|
|
|
|
|
|
import java.util.Arrays;
|
|
|
|
|
|
|
|
public class CableConverter implements MultiPartRegistry.IPartConverter {
|
|
|
|
@Override
|
|
|
|
public Iterable<Block> blockTypes() {
|
|
|
|
return Arrays.asList(Block.getBlockFromItem(IC2Items.getItem("copperCableBlock").getItem()));
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public TMultiPart convert(World world, BlockCoord blockCoord) {
|
|
|
|
Block block = world.getBlock(blockCoord.x, blockCoord.y, blockCoord.z);
|
|
|
|
if (block instanceof BlockCable) {
|
|
|
|
TileEntity tileEntity = world.getTileEntity(blockCoord.x, blockCoord.y, blockCoord.z);
|
|
|
|
if (tileEntity instanceof TileEntityCable) {
|
|
|
|
TileEntityCable cable = (TileEntityCable) tileEntity;
|
|
|
|
int type = cable.cableType;
|
|
|
|
CablePart newPart = new CablePart();
|
|
|
|
newPart.setType(type);
|
|
|
|
return new FMPModPart(newPart);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
2015-10-23 17:04:56 +02:00
|
|
|
|
|
|
|
private ThreadLocal<Object> placing = new ThreadLocal<Object>();
|
|
|
|
|
|
|
|
@SubscribeEvent(priority = EventPriority.LOW)
|
2015-11-08 13:15:45 +01:00
|
|
|
public void playerInteract(PlayerInteractEvent event) {
|
|
|
|
if (event.action == PlayerInteractEvent.Action.RIGHT_CLICK_BLOCK && event.entityPlayer.worldObj.isRemote) {
|
|
|
|
if (placing.get() != null)
|
2015-10-23 17:04:56 +02:00
|
|
|
return;
|
|
|
|
placing.set(event);
|
2015-11-08 13:15:45 +01:00
|
|
|
if (place(event.entityPlayer, event.entityPlayer.worldObj))
|
2015-10-23 17:04:56 +02:00
|
|
|
event.setCanceled(true);
|
|
|
|
placing.set(null);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-08 13:15:45 +01:00
|
|
|
public static boolean place(EntityPlayer player, World world) {
|
2015-10-23 17:04:56 +02:00
|
|
|
MovingObjectPosition hit = RayTracer.reTrace(world, player);
|
2015-11-08 13:15:45 +01:00
|
|
|
if (hit == null)
|
2015-10-23 17:04:56 +02:00
|
|
|
return false;
|
|
|
|
|
|
|
|
BlockCoord pos = new BlockCoord(hit.blockX, hit.blockY, hit.blockZ).offset(hit.sideHit);
|
|
|
|
ItemStack held = player.getHeldItem();
|
|
|
|
FMPModPart part = null;
|
2015-11-08 13:15:45 +01:00
|
|
|
if (held == null)
|
2015-10-23 17:04:56 +02:00
|
|
|
return false;
|
|
|
|
|
|
|
|
Item heldItem = held.getItem();
|
|
|
|
|
|
|
|
|
2015-11-08 13:15:45 +01:00
|
|
|
if (heldItem == IC2Items.getItem("copperCableItem").getItem())
|
2015-10-23 17:04:56 +02:00
|
|
|
part = placeCable(world, pos, hit.sideHit, held);
|
|
|
|
|
2015-11-08 13:15:45 +01:00
|
|
|
if (part == null)
|
2015-10-23 17:04:56 +02:00
|
|
|
return false;
|
|
|
|
|
2015-11-08 13:15:45 +01:00
|
|
|
if (world.isRemote && !player.isSneaking())//attempt to use block activated like normal and tell the server the right stuff
|
2015-10-23 17:04:56 +02:00
|
|
|
{
|
|
|
|
Vector3 f = new Vector3(hit.hitVec).add(-hit.blockX, -hit.blockY, -hit.blockZ);
|
|
|
|
Block block = world.getBlock(hit.blockX, hit.blockY, hit.blockZ);
|
2015-11-08 13:15:45 +01:00
|
|
|
if (!ignoreActivate(block) && block.onBlockActivated(world, hit.blockX, hit.blockY, hit.blockZ, player, hit.sideHit, (float) f.x, (float) f.y, (float) f.z)) {
|
2015-10-23 17:04:56 +02:00
|
|
|
player.swingItem();
|
|
|
|
PacketCustom.sendToServer(new C08PacketPlayerBlockPlacement(
|
|
|
|
hit.blockX, hit.blockY, hit.blockZ, hit.sideHit,
|
|
|
|
player.inventory.getCurrentItem(),
|
|
|
|
(float) f.x, (float) f.y, (float) f.z));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
TileMultipart tile = TileMultipart.getOrConvertTile(world, pos);
|
2015-11-08 13:15:45 +01:00
|
|
|
if (tile == null || !tile.canAddPart(part))
|
2015-10-23 17:04:56 +02:00
|
|
|
return false;
|
|
|
|
|
2015-11-08 13:15:45 +01:00
|
|
|
if (!world.isRemote) {
|
2015-10-23 17:04:56 +02:00
|
|
|
TileMultipart.addPart(world, pos, part);
|
|
|
|
world.playSoundEffect(pos.x + 0.5, pos.y + 0.5, pos.z + 0.5,
|
|
|
|
Blocks.wool.stepSound.func_150496_b(),
|
|
|
|
(Blocks.wool.stepSound.getVolume() + 1.0F) / 2.0F,
|
|
|
|
Blocks.wool.stepSound.getPitch() * 0.8F);
|
2015-11-08 13:15:45 +01:00
|
|
|
if (!player.capabilities.isCreativeMode) {
|
2015-10-23 17:04:56 +02:00
|
|
|
held.stackSize--;
|
2015-11-08 13:15:45 +01:00
|
|
|
if (held.stackSize == 0) {
|
2015-10-23 17:04:56 +02:00
|
|
|
player.inventory.mainInventory[player.inventory.currentItem] = null;
|
|
|
|
MinecraftForge.EVENT_BUS.post(new PlayerDestroyItemEvent(player, held));
|
|
|
|
}
|
|
|
|
}
|
2015-11-08 13:15:45 +01:00
|
|
|
} else {
|
2015-10-23 17:04:56 +02:00
|
|
|
player.swingItem();
|
2015-11-08 13:15:45 +01:00
|
|
|
// new PacketCustom(McMultipartSPH.channel, 1).sendToServer();
|
2015-10-23 17:04:56 +02:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-11-08 13:15:45 +01:00
|
|
|
private static boolean ignoreActivate(Block block) {
|
|
|
|
if (block instanceof BlockFence)
|
2015-10-23 17:04:56 +02:00
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-11-08 13:15:45 +01:00
|
|
|
public static FMPModPart placeCable(World world, BlockCoord pos, int side, ItemStack held) {
|
|
|
|
if (side == 0)
|
2015-10-23 17:04:56 +02:00
|
|
|
return null;
|
2015-11-08 13:15:45 +01:00
|
|
|
pos = pos.copy().offset(side ^ 1);
|
2015-10-23 17:04:56 +02:00
|
|
|
Block block = world.getBlock(pos.x, pos.y, pos.z);
|
2015-11-08 13:15:45 +01:00
|
|
|
if (!block.isSideSolid(world, pos.x, pos.y, pos.z, ForgeDirection.getOrientation(side)) && (side != 1 || block.canPlaceTorchOnTop(world, pos.x, pos.y, pos.z)))
|
2015-10-23 17:04:56 +02:00
|
|
|
return null;
|
|
|
|
|
|
|
|
CablePart part = new CablePart();
|
|
|
|
part.setType(held.getItemDamage() * 16);
|
|
|
|
return new FMPModPart(part);
|
|
|
|
}
|
|
|
|
|
2015-10-05 18:39:45 +02:00
|
|
|
}
|