From 6b6bd0bdd0d59fd63a9ce10a3a707f234c29d926 Mon Sep 17 00:00:00 2001 From: modmuss50 Date: Fri, 23 Oct 2015 16:04:56 +0100 Subject: [PATCH] Some small changes to the multi parts --- .../techreborn/partSystem/fmp/FMPFactory.java | 2 + .../techreborn/partSystem/fmp/FMPModPart.java | 16 +++ .../partSystem/parts/CableConverter.java | 122 ++++++++++++++++++ 3 files changed, 140 insertions(+) diff --git a/src/main/java/techreborn/partSystem/fmp/FMPFactory.java b/src/main/java/techreborn/partSystem/fmp/FMPFactory.java index ea2ae5fb2..f32fd5a9e 100644 --- a/src/main/java/techreborn/partSystem/fmp/FMPFactory.java +++ b/src/main/java/techreborn/partSystem/fmp/FMPFactory.java @@ -18,6 +18,7 @@ import net.minecraft.item.ItemStack; import net.minecraft.nbt.NBTTagCompound; import net.minecraft.tileentity.TileEntity; import net.minecraft.world.World; +import net.minecraftforge.common.MinecraftForge; import techreborn.lib.Location; import techreborn.lib.vecmath.Vecs3dCube; import techreborn.partSystem.IModPart; @@ -80,6 +81,7 @@ public class FMPFactory implements MultiPartRegistry.IPartFactory2, public void init() { if(Loader.isModLoaded("IC2")){ MultiPartRegistry.registerConverter(new CableConverter()); + MinecraftForge.EVENT_BUS.register(new CableConverter()); } } diff --git a/src/main/java/techreborn/partSystem/fmp/FMPModPart.java b/src/main/java/techreborn/partSystem/fmp/FMPModPart.java index 1cb0a8c69..f773dd8a5 100644 --- a/src/main/java/techreborn/partSystem/fmp/FMPModPart.java +++ b/src/main/java/techreborn/partSystem/fmp/FMPModPart.java @@ -210,4 +210,20 @@ public class FMPModPart extends TMultiPart implements TSlottedPart, packet.writeNBTTagCompound(tagCompound); } } + + @Override + public void onWorldJoin() { + if (iModPart.world == null || iModPart.location == null) { + iModPart.setWorld(world()); + iModPart.setLocation(new Location(x(), y(), z())); + } + iModPart.onAdded(); + } + + @Override + public void onWorldSeparate() { + iModPart.onChunkUnload(); + } + + } diff --git a/src/main/java/techreborn/partSystem/parts/CableConverter.java b/src/main/java/techreborn/partSystem/parts/CableConverter.java index c7212a2ff..130b7c042 100644 --- a/src/main/java/techreborn/partSystem/parts/CableConverter.java +++ b/src/main/java/techreborn/partSystem/parts/CableConverter.java @@ -1,14 +1,33 @@ package techreborn.partSystem.parts; +import codechicken.lib.packet.PacketCustom; +import codechicken.lib.raytracer.RayTracer; import codechicken.lib.vec.BlockCoord; +import codechicken.lib.vec.Vector3; import codechicken.multipart.MultiPartRegistry; import codechicken.multipart.TMultiPart; +import codechicken.multipart.TileMultipart; +import codechicken.multipart.minecraft.McBlockPart; +import codechicken.multipart.minecraft.McMultipartSPH; +import cpw.mods.fml.common.eventhandler.EventPriority; +import cpw.mods.fml.common.eventhandler.SubscribeEvent; import ic2.api.item.IC2Items; import ic2.core.block.wiring.BlockCable; import ic2.core.block.wiring.TileEntityCable; import net.minecraft.block.Block; +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; import net.minecraft.tileentity.TileEntity; +import net.minecraft.util.MovingObjectPosition; import net.minecraft.world.World; +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; import techreborn.partSystem.fmp.FMPModPart; import java.util.Arrays; @@ -34,4 +53,107 @@ public class CableConverter implements MultiPartRegistry.IPartConverter { } return null; } + + private ThreadLocal placing = new ThreadLocal(); + + @SubscribeEvent(priority = EventPriority.LOW) + public void playerInteract(PlayerInteractEvent event) + { + if(event.action == PlayerInteractEvent.Action.RIGHT_CLICK_BLOCK && event.entityPlayer.worldObj.isRemote) + { + if(placing.get() != null) + return; + placing.set(event); + if(place(event.entityPlayer, event.entityPlayer.worldObj)) + event.setCanceled(true); + placing.set(null); + } + } + + public static boolean place(EntityPlayer player, World world) + { + MovingObjectPosition hit = RayTracer.reTrace(world, player); + if(hit == null) + return false; + + BlockCoord pos = new BlockCoord(hit.blockX, hit.blockY, hit.blockZ).offset(hit.sideHit); + ItemStack held = player.getHeldItem(); + FMPModPart part = null; + if(held == null) + return false; + + Item heldItem = held.getItem(); + + + if(heldItem == IC2Items.getItem("copperCableItem").getItem()) + part = placeCable(world, pos, hit.sideHit, held); + + if(part == null) + return false; + + if(world.isRemote && !player.isSneaking())//attempt to use block activated like normal and tell the server the right stuff + { + Vector3 f = new Vector3(hit.hitVec).add(-hit.blockX, -hit.blockY, -hit.blockZ); + Block block = world.getBlock(hit.blockX, hit.blockY, hit.blockZ); + if(!ignoreActivate(block) && block.onBlockActivated(world, hit.blockX, hit.blockY, hit.blockZ, player, hit.sideHit, (float)f.x, (float)f.y, (float)f.z)) + { + 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); + if(tile == null || !tile.canAddPart(part)) + return false; + + if(!world.isRemote) + { + 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); + if(!player.capabilities.isCreativeMode) + { + held.stackSize--; + if (held.stackSize == 0) + { + player.inventory.mainInventory[player.inventory.currentItem] = null; + MinecraftForge.EVENT_BUS.post(new PlayerDestroyItemEvent(player, held)); + } + } + } + else + { + player.swingItem(); + // new PacketCustom(McMultipartSPH.channel, 1).sendToServer(); + } + return true; + } + + private static boolean ignoreActivate(Block block) + { + if(block instanceof BlockFence) + return true; + return false; + } + + public static FMPModPart placeCable(World world, BlockCoord pos, int side, ItemStack held) + { + if(side == 0) + return null; + pos = pos.copy().offset(side^1); + Block block = world.getBlock(pos.x, pos.y, pos.z); + if(!block.isSideSolid(world, pos.x, pos.y, pos.z, ForgeDirection.getOrientation(side)) && (side != 1 || block.canPlaceTorchOnTop(world, pos.x, pos.y, pos.z))) + return null; + + CablePart part = new CablePart(); + part.setType(held.getItemDamage() * 16); + return new FMPModPart(part); + } + }