Tschipp Posted August 14, 2016 Posted August 14, 2016 Hi I'm trying to make a wand tool, similar to worldedit. I am saving the material of the wand into NBT but not pos1 and pos2, as I want the player to be able to use multiple wands that share positions, but not materials. Now I just run into the problem that when using the wand on a server, all players share the same pos1 and pos2, causing major problems. How do I make the positions individual for each player, but not for each wand? Should I edit the player's nbt? Here is the code that I have so far: (I know it's a mess, please bare with me : package tschipp.creativePlus.items; import java.util.List; import tschipp.creativePlus.CreativePlus; import net.minecraft.block.Block; import net.minecraft.block.state.BlockState; import net.minecraft.block.state.IBlockState; import net.minecraft.client.Minecraft; import net.minecraft.client.resources.Language; import net.minecraft.creativetab.CreativeTabs; import net.minecraft.entity.Entity; import net.minecraft.entity.EntityLivingBase; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.init.Blocks; import net.minecraft.item.Item; import net.minecraft.item.ItemStack; import net.minecraft.nbt.NBTTagCompound; import net.minecraft.util.BlockPos; import net.minecraft.util.ChatComponentText; import net.minecraft.util.EnumFacing; import net.minecraft.util.StatCollector; import net.minecraft.util.Vec3; import net.minecraft.world.World; import net.minecraftforge.fml.relauncher.Side; import net.minecraftforge.fml.relauncher.SideOnly; public class Wand extends Item{ public Vec3 pos1; public Vec3 pos2; public Vec3 difference; public Vec3 posToPlaceBlock; public IBlockState mat; public Wand() { this.setMaxStackSize(1); } @Override @SideOnly(Side.CLIENT) public void getSubItems(Item item, CreativeTabs tab, List<ItemStack> subItems) { NBTTagCompound tag = new NBTTagCompound(); tag.setString("material", "minecraft:stone"); tag.setInteger("damage", 0); ItemStack stack = new ItemStack(item, 1, 0); stack.setTagCompound(tag); subItems.add(stack); } public boolean onItemUse(ItemStack stack, EntityPlayer player, World worldIn, BlockPos pos, EnumFacing side, float hitX, float hitY, float hitZ) { World world = player.worldObj; if(pos.getX() < 0) { pos2 = new Vec3(pos.getX()-0.5, pos.getY(), pos.getZ()); } if(pos.getX() > 0) { pos2 = new Vec3(pos.getX()+0.5, pos.getY(), pos.getZ()); } if(pos.getZ() < 0) { pos2 = new Vec3(pos.getX(), pos.getY(), pos.getZ()-0.5); } if(pos.getX() > 0) { pos2 = new Vec3(pos.getX()-0.5, pos.getY(), pos.getZ()+0.5); } if(pos.getX() < 0 && pos.getZ() < 0) { pos2 = new Vec3(pos.getX()-0.5, pos.getY(), pos.getZ()-0.5); } if(pos.getX() > 0 && pos.getZ() > 0) { pos2 = new Vec3(pos.getX()+0.5, pos.getY(), pos.getZ()+0.5); } if(pos.getX() < 0 && pos.getZ() > 0) { pos2 = new Vec3(pos.getX()-0.5, pos.getY(), pos.getZ()+0.5); } if(pos.getX() > 0 && pos.getZ() < 0) { pos2 = new Vec3(pos.getX()+0.5, pos.getY(), pos.getZ()-0.5); } if(!world.isRemote) { player.addChatComponentMessage(new ChatComponentText("§dSet Pos2 at X: " + (int)pos2.xCoord + ", Y: " + (int)pos2.yCoord + ", Z: " + (int)pos2.zCoord)); } world.markBlockForUpdate(pos); return false; } public ItemStack onItemRightClick(ItemStack stack, World worldIn, EntityPlayer playerIn) { if(pos1 != null && pos2 != null && playerIn.isSneaking()) { difference = pos1.subtractReverse(pos2).normalize(); posToPlaceBlock = pos1; World world = Minecraft.getMinecraft().getIntegratedServer().getEntityWorld(); if(compare()) { pos1 = null; if(!world.isRemote) { world.setBlockState(new BlockPos((int)pos1.xCoord, (int)pos1.yCoord, (int)pos1.zCoord), Block.getBlockFromName(stack.getTagCompound().getString("material")).getStateFromMeta(stack.getTagCompound().getInteger("damage")), 2); } pos2 = null; } else { while (!compare()) { posToPlaceBlock = posToPlaceBlock.add(difference); if(!world.isRemote) { world.setBlockState(new BlockPos((int)posToPlaceBlock.xCoord, (int)posToPlaceBlock.yCoord, (int)posToPlaceBlock.zCoord), Block.getBlockFromName(stack.getTagCompound().getString("material")).getStateFromMeta(stack.getTagCompound().getInteger("damage")), 2); } } } } return stack; } public boolean compare() { if(Math.abs(pos1.xCoord) <= Math.abs(pos2.xCoord) && Math.abs(pos1.zCoord) <= Math.abs(pos2.zCoord)) { return Math.abs(posToPlaceBlock.xCoord) >= Math.abs(pos2.xCoord) && Math.abs(posToPlaceBlock.zCoord) >= Math.abs(pos2.zCoord); } else if(Math.abs(pos1.xCoord) >= Math.abs(pos2.xCoord) && Math.abs(pos1.zCoord) >= Math.abs(pos2.zCoord)) { return Math.abs(posToPlaceBlock.xCoord) <= Math.abs(pos2.xCoord) && Math.abs(posToPlaceBlock.zCoord) <= Math.abs(pos2.zCoord); } else if(Math.abs(pos1.xCoord) <= Math.abs(pos2.xCoord) && Math.abs(pos1.zCoord) >= Math.abs(pos2.zCoord)) { return Math.abs(posToPlaceBlock.xCoord) >= Math.abs(pos2.xCoord) && Math.abs(posToPlaceBlock.zCoord) <= Math.abs(pos2.zCoord); } else if(Math.abs(pos1.xCoord) >= Math.abs(pos2.xCoord) && Math.abs(pos1.zCoord) <= Math.abs(pos2.zCoord)) { return Math.abs(posToPlaceBlock.xCoord) <= Math.abs(pos2.xCoord) && Math.abs(posToPlaceBlock.zCoord) >= Math.abs(pos2.zCoord); } else { return true; } } public boolean onBlockStartBreak(ItemStack itemstack, BlockPos pos, EntityPlayer player) { World world = player.worldObj; if(player.isSneaking()) { mat = world.getBlockState(pos); NBTTagCompound tag = new NBTTagCompound(); tag.setString("material", mat.getBlock().getRegistryName()); tag.setInteger("damage", mat.getBlock().getMetaFromState(mat)); itemstack.setTagCompound(tag); if(!world.isRemote) { player.addChatComponentMessage(new ChatComponentText("§dMaterial set to: " + StatCollector.translateToLocal(StatCollector.translateToLocal(Block.getBlockFromName(itemstack.getTagCompound().getString("material")).getLocalizedName())))); } world.markBlockForUpdate(pos); } else { if(pos.getX() < 0) { pos1 = new Vec3(pos.getX()-0.5, pos.getY(), pos.getZ()); } if(pos.getX() > 0) { pos1 = new Vec3(pos.getX()+0.5, pos.getY(), pos.getZ()); } if(pos.getZ() < 0) { pos1 = new Vec3(pos.getX(), pos.getY(), pos.getZ()-0.5); } if(pos.getX() > 0) { pos1 = new Vec3(pos.getX()-0.5, pos.getY(), pos.getZ()+0.5); } if(pos.getX() < 0 && pos.getZ() < 0) { pos1 = new Vec3(pos.getX()-0.5, pos.getY(), pos.getZ()-0.5); } if(pos.getX() > 0 && pos.getZ() > 0) { pos1 = new Vec3(pos.getX()+0.5, pos.getY(), pos.getZ()+0.5); } if(pos.getX() < 0 && pos.getZ() > 0) { pos1 = new Vec3(pos.getX()-0.5, pos.getY(), pos.getZ()+0.5); } if(pos.getX() > 0 && pos.getZ() < 0) { pos1 = new Vec3(pos.getX()+0.5, pos.getY(), pos.getZ()-0.5); } if(!world.isRemote) { player.addChatComponentMessage(new ChatComponentText("§dSet Pos1 at X: " + (int)pos1.xCoord + ", Y: " + (int)pos1.yCoord + ", Z: " + (int)pos1.zCoord)); } world.markBlockForUpdate(pos); } return true; } @Override @SideOnly(Side.CLIENT) public void addInformation(ItemStack stack, EntityPlayer playerIn, List<String> tooltip, boolean advanced) { tooltip.add("Draws a straight line from two points"); tooltip.add("Material: "+ StatCollector.translateToLocal(Block.getBlockFromName(stack.getTagCompound().getString("material")).getLocalizedName()) + ", Meta: " + stack.getTagCompound().getInteger("damage")); } } Quote
Animefan8888 Posted August 14, 2016 Posted August 14, 2016 Update to 1.10 and use the capability system. Quote VANILLA MINECRAFT CLASSES ARE THE BEST RESOURCES WHEN MODDING I will be posting 1.15.2 modding tutorials on this channel. If you want to be notified of it do the normal YouTube stuff like subscribing, ect. Forge and vanilla BlockState generator.
Tschipp Posted August 14, 2016 Author Posted August 14, 2016 Update to 1.10 and use the capability system. Lol Any Idea how to do this in 1.8? Quote
Animefan8888 Posted August 14, 2016 Posted August 14, 2016 1.8 also uses the Capability system. Quote VANILLA MINECRAFT CLASSES ARE THE BEST RESOURCES WHEN MODDING I will be posting 1.15.2 modding tutorials on this channel. If you want to be notified of it do the normal YouTube stuff like subscribing, ect. Forge and vanilla BlockState generator.
Tschipp Posted August 14, 2016 Author Posted August 14, 2016 1.8 also uses the Capability system. So how would that look like? Are you talking about NBT? Quote
Animefan8888 Posted August 14, 2016 Posted August 14, 2016 Sorry for not providing this in my last post. http://mcforge.readthedocs.io/en/latest/datastorage/capabilities/ Quote VANILLA MINECRAFT CLASSES ARE THE BEST RESOURCES WHEN MODDING I will be posting 1.15.2 modding tutorials on this channel. If you want to be notified of it do the normal YouTube stuff like subscribing, ect. Forge and vanilla BlockState generator.
Choonster Posted August 14, 2016 Posted August 14, 2016 You can't use the Minecraft class in common code, it's client-only. You're already provided the World as an argument of your Item#onItemRightClick and Item#onItemUse overrides, use this instead of trying to get it from the integrated server. Item s are singletons, you can't store per-item data in fields of your Item class. You need to store this data in the ItemStack using metadata, NBT or capabilities as appropriate. Always annotate override methods with @Override so you get a compilation error if they don't actually override a super method. Why are you using Vec3 s to store block positions? Just use BlockPos . You can find the official documentation for the capability system here. Forge itself has several capability examples, look at the usages of CapabilityManager.register in your IDE. There's also a test mod here. I have some examples here: API, implementation Quote Please don't PM me to ask for help. Asking your question in a public thread preserves it for people who are having the same problem in the future.
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.