Jump to content

[1.8.9] Conflicting positions for SP and MP


Tschipp

Recommended Posts

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"));

                }


}

Link to comment
Share on other sites

Sorry for not providing this in my last post.

http://mcforge.readthedocs.io/en/latest/datastorage/capabilities/

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.

Link to comment
Share on other sites

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

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.

Link to comment
Share on other sites

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.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • The error code shows that i have outdated mods, but only coFH core crashes my game and it's not outdated + i double checked and its on the right version of forge. -- Head -- Thread: Render thread Stacktrace:     at cofh.core.client.PostEffect.m_6213_(PostEffect.java:80) ~[cofh_core-1.20.1-11.0.2.56.jar%23218!/:11.0.2] {re:mixin,re:classloading}     at cofh.core.client.PostBuffer.m_6213_(PostBuffer.java:96) ~[cofh_core-1.20.1-11.0.2.56.jar%23218!/:11.0.2] {re:classloading}     at net.minecraft.server.packs.resources.ResourceManagerReloadListener.m_10759_(ResourceManagerReloadListener.java:15) ~[client-1.20.1-20230612.114412-srg.jar%23267!/:?] {re:computing_frames,re:classloading,re:mixin}     at java.util.concurrent.CompletableFuture$UniRun.tryFire(CompletableFuture.java:787) ~[?:?] {}     at java.util.concurrent.CompletableFuture$Completion.run(CompletableFuture.java:482) ~[?:?] {}     at net.minecraft.server.packs.resources.SimpleReloadInstance.m_143940_(SimpleReloadInstance.java:69) ~[client-1.20.1-20230612.114412-srg.jar%23267!/:?] {re:classloading}     at net.minecraft.util.thread.BlockableEventLoop.m_6367_(BlockableEventLoop.java:198) ~[client-1.20.1-20230612.114412-srg.jar%23267!/:?] {re:mixin,pl:accesstransformer:B,xf:OptiFine:default,re:computing_frames,pl:accesstransformer:B,xf:OptiFine:default,re:classloading,pl:accesstransformer:B,xf:OptiFine:default}     at net.minecraft.util.thread.ReentrantBlockableEventLoop.m_6367_(ReentrantBlockableEventLoop.java:23) ~[client-1.20.1-20230612.114412-srg.jar%23267!/:?] {re:mixin,re:computing_frames,re:classloading}     at net.minecraft.util.thread.BlockableEventLoop.m_7245_(BlockableEventLoop.java:163) ~[client-1.20.1-20230612.114412-srg.jar%23267!/:?] {re:mixin,pl:accesstransformer:B,xf:OptiFine:default,re:computing_frames,pl:accesstransformer:B,xf:OptiFine:default,re:classloading,pl:accesstransformer:B,xf:OptiFine:default} -- Overlay render details -- Details:     Overlay name: net.minecraftforge.client.loading.ForgeLoadingOverlay Stacktrace:     at net.minecraft.client.renderer.GameRenderer.m_109093_(GameRenderer.java:1385) ~[client-1.20.1-20230612.114412-srg.jar%23267!/:?] {re:mixin,pl:accesstransformer:B,xf:OptiFine:default,re:classloading,pl:accesstransformer:B,xf:OptiFine:default,pl:mixin:APP:moonlight-common.mixins.json:GameRendererMixin,pl:mixin:APP:supplementaries-common.mixins.json:GameRendererMixin,pl:mixin:APP:mixins.cofhcore.json:GameRendererMixin,pl:mixin:APP:create.mixins.json:accessor.GameRendererAccessor,pl:mixin:APP:create.mixins.json:client.GameRendererMixin,pl:mixin:A}     at net.minecraft.client.Minecraft.m_91383_(Minecraft.java:1146) ~[client-1.20.1-20230612.114412-srg.jar%23267!/:?] {re:mixin,pl:accesstransformer:B,pl:runtimedistcleaner:A,re:classloading,pl:accesstransformer:B,pl:mixin:A,pl:runtimedistcleaner:A}     at net.minecraft.client.Minecraft.m_91374_(Minecraft.java:718) ~[client-1.20.1-20230612.114412-srg.jar%23267!/:?] {re:mixin,pl:accesstransformer:B,pl:runtimedistcleaner:A,re:classloading,pl:accesstransformer:B,pl:mixin:A,pl:runtimedistcleaner:A}     at net.minecraft.client.main.Main.main(Main.java:218) ~[1.20.1-forge-47.3.0.jar:?] {re:mixin,pl:runtimedistcleaner:A,re:classloading,pl:mixin:APP:flywheel.mixins.json:ClientMainMixin,pl:mixin:A,pl:runtimedistcleaner:A}     at jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:?] {}     at jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77) ~[?:?] {}     at jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:?] {}     at java.lang.reflect.Method.invoke(Method.java:568) ~[?:?] {}     at net.minecraftforge.fml.loading.targets.CommonLaunchHandler.runTarget(CommonLaunchHandler.java:111) ~[fmlloader-1.20.1-47.3.0.jar:?] {}     at net.minecraftforge.fml.loading.targets.CommonLaunchHandler.clientService(CommonLaunchHandler.java:99) ~[fmlloader-1.20.1-47.3.0.jar:?] {}     at net.minecraftforge.fml.loading.targets.CommonClientLaunchHandler.lambda$makeService$0(CommonClientLaunchHandler.java:25) ~[fmlloader-1.20.1-47.3.0.jar:?] {}     at cpw.mods.modlauncher.LaunchServiceHandlerDecorator.launch(LaunchServiceHandlerDecorator.java:30) ~[modlauncher-10.0.9.jar:?] {}     at cpw.mods.modlauncher.LaunchServiceHandler.launch(LaunchServiceHandler.java:53) ~[modlauncher-10.0.9.jar:?] {}     at cpw.mods.modlauncher.LaunchServiceHandler.launch(LaunchServiceHandler.java:71) ~[modlauncher-10.0.9.jar:?] {}     at cpw.mods.modlauncher.Launcher.run(Launcher.java:108) ~[modlauncher-10.0.9.jar:?] {}     at cpw.mods.modlauncher.Launcher.main(Launcher.java:78) ~[modlauncher-10.0.9.jar:?] {}     at cpw.mods.modlauncher.BootstrapLaunchConsumer.accept(BootstrapLaunchConsumer.java:26) ~[modlauncher-10.0.9.jar:?] {}     at cpw.mods.modlauncher.BootstrapLaunchConsumer.accept(BootstrapLaunchConsumer.java:23) ~[modlauncher-10.0.9.jar:?] {}     at cpw.mods.bootstraplauncher.BootstrapLauncher.main(BootstrapLauncher.java:141) ~[bootstraplauncher-1.1.2.jar:?] {}     at jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:?] {}     at jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77) ~[?:?] {}     at jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:?] {}     at java.lang.reflect.Method.invoke(Method.java:568) ~[?:?] {}     at crystallauncher.MinecraftConstructor.run(MinecraftConstructor.java:29) ~[proxyserver.jar%2380!/:?] {}     at crystallauncher.MineClient.start(MineClient.java:201) ~[proxyserver.jar%2380!/:?] {}     at crystallauncher.MineClient.main(MineClient.java:42) ~[proxyserver.jar%2380!/:?] {}
    • Update your AMD/ATI drivers - get the drivers from their website - do not update via system
    • Need help with making shooting book, in this code i can summon fireball but it goes inside or oposite way can you help me? package net.LimboTeam.tropicmod.item.custom; import net.LimboTeam.tropicmod.item.ModCreativeModTab; import net.minecraft.sounds.SoundEvents; import net.minecraft.sounds.SoundSource; import net.minecraft.world.InteractionHand; import net.minecraft.world.InteractionResult; import net.minecraft.world.InteractionResultHolder; import net.minecraft.world.entity.player.Player; import net.minecraft.world.entity.projectile.LargeFireball; import net.minecraft.world.item.Item; import net.minecraft.world.item.ItemStack; import net.minecraft.world.level.Level; import net.minecraft.world.phys.Vec3; import java.util.Random; public class FireballMagicBook extends Item { public FireballMagicBook(Properties properties) { super(new Item.Properties().tab(ModCreativeModTab.TropicModTab)); } @Override public InteractionResultHolder<ItemStack> use(Level level, Player player, InteractionHand hand) { ItemStack itemStack = player.getItemInHand(hand); Random random = new Random(); Vec3 vec3 = player.getViewVector(1.0f); if (!level.isClientSide && hand == InteractionHand.MAIN_HAND){ double X = player.getX() - (player.getX() + vec3.x * 4.0); double Y = player.getY(0.5) - (0.5 + player.getY(0.5)); double Z = player.getZ() - (player.getZ() + vec3.z * 4.0); LargeFireball fireball = new LargeFireball(level, player, X , Y, Z,1); fireball.setPos(player.getX(), player.getY(), player.getZ()); level.addFreshEntity(fireball); level.playSound((Player)null, player.getX(), player.getY(), player.getZ(), SoundEvents.GHAST_SHOOT, SoundSource.NEUTRAL, 0.5F, 0.4F / (random.nextFloat() * 0.4F + 0.8F)); if (!player.isCreative()) { itemStack.setDamageValue(itemStack.getDamageValue() + 1); } player.getCooldowns().addCooldown(this, 40 ); return new InteractionResultHolder<>(InteractionResult.SUCCESS, itemStack); } return new InteractionResultHolder<>(InteractionResult.FAIL, itemStack); } }  
  • Topics

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.