Jump to content

Topic: My code is acting weird.


Pythonschlange

Recommended Posts

Well my Problem is that whenever I activate the onItemUse event the block, I Clicked on, get's replaced with air and the sound get's played, just as planed, but right after that the air block get´s replaced again with the block that was there before and i don't know why MC does that :-\.I want the item to work once but without removing or destroying it. (sorry for probably bad English I'm no native speaker)

Here is my Code:

 

package items;

import org.Python.StuffForMC.StuffForMC;

import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import net.minecraft.block.Block;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.init.Blocks;
import net.minecraft.init.Items;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.util.DamageSource;
import net.minecraft.world.World;

public class Item_Work_Scope extends Item {


public Item_Work_Scope(){
	setCreativeTab(StuffForMC.tabStuffForMC);
	setMaxStackSize(1);
	setUnlocalizedName("Item_Work_Scope");
	setTextureName("StuffForMC:WorkScope");
	}
 @SideOnly(Side.CLIENT)
    public boolean isFull3D()
    {
        return true;
    }
 	private static int test = 1;
	public boolean onItemUse(ItemStack itemStack, EntityPlayer entity, World world, int x, int y, int z, int l, float a, float b, float c){
		float var4 = 1.0F;
		Block g = Blocks.bedrock;
		Block block = world.getBlock(x, y, z);
		if(test<2){
		if (block != g)
		{
			test=test+1;
			world.setBlock(x, y, z, Blocks.air);
			world.playRecord("mob.zombie.woodbreak", x, y, z);
			}
		}
		return true;
		}}

 

Link to comment
Share on other sites

There is only ever one instance of your Item in existence. So even if you were to make the field non-static it would be the same for all of your Items. If you want to store something, you need to store it in the ItemStack. Either as the damage value or in the NBT tag.

I'm sorry but I really don't get what you're trying to say.

If you set the Block on the client exactly what you experienced happened: The block is not really there, you cannot interact with it and so on. When you modify the world you always need to do it on the server.

I don't see the problem in the Cliend or Server thing, because it works fine without all "test" variables. If I remove all "test"variables it works perfectly, but infinitly often and I want it to work just once. So i figured this variable thing out that was supposed to limit the onItemUse event to one use. That seems to work fine as well, untill the if part closes of and something decides to undo enything that happens in the if part.

Link to comment
Share on other sites

Do you know what static means? The variable exists ONLY ONCE. So if two players hold your item, the variable is still the same for both of those.

Oh.. I thought it was ment to just generate once. So I see my mistake here.

Even if you make it non-static that will still apply.

Ok...then how am i able to solve that problem?

Link to comment
Share on other sites

Item is something like "what is this thing I am holding?"

ItemStack is the "THING I am holding."

 

Item only tells game that stuff you hold should be treated as something.

ItemStack actually holds data - it's itemDamage and/or NBT.

 

Google NBT and how to work with it (a lot of tuts).

1.7.10 is no longer supported by forge, you are on your own.

Link to comment
Share on other sites

Oke, so you got to remember that the server is responsible for all the desicion makings etc. While the Client only renders the world. Since the items has logic(= server) and rendering(= Client), you need to decide what should be called where. In case of the blockPlacement you need to check if the method is called by the server side, otherwise the blockchanging would be only a visible effect, not a physical one.

 

Add this line to  check if its the server who calls

//if(!World.isremote)
if(!world.isremote)
{
    world.setBlock(x, y, z, Blocks.air);
}

 

Edit:

Thanks for noticing. I was typing on a mobile phone, and totaly missed that accidential typed capital.

Projects:

Discontinued:

- N2ConfigAPI

- Meachanical Crafting Table

 

Latest:

- CollectionUtils

 

Coöperations:

- InGameConfigManager

Link to comment
Share on other sites

Oke, so you got to remember that the server is responsible for all the desicion makings etc. While the Client only renders the world. Since the items has logic(= server) and rendering(= Client), you need to decide what should be called where. In case of the blockPlacement you need to check if the method is called by the server side, otherwise the blockchanging would be only a visible effect, not a physical one.

 

Add this line to  check if its the server who calls

if(!World.isremote)
{
    world.setBlock(x, y, z, Blocks.air);
}

 

World != world if anyone is confused

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.

Announcements



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • https://drive.google.com/file/d/1M0EG-c5yfRd08DSnE5HVQNCu2v0YtA7A/view?usp=sharing  
    • so im not sure if your still watching this TileEntity but once we loaded a FTB modpack we wanted to run on the server it no longer seems to run. The console opens like normal but we've been sitting here for like a little over an hour and the server screen has not yet opened. Should we just keep waiting at this point?
    • I have now easily fixed the duplication error present, I was just not looking.   I have been working for the past half hour to try and fix another error present, this time with the Creative Mode Tab.   I have changed some things around to get where I am currently. (ModFoods to ModDrinks*) and it cannot find the symbol ".get" at the end of the code. *The custom class you recommended pOutput.accept(ModDrinks.ORANGE_JUICE.get()); I think the point I am at currently is the closest I have to how it should be but because I am not as experienced with java I would not know.  I have also removed ORANGE_JUICE and LEMON_JUICE from the ModFoods class, to avoid confliction. I do hope all this can be fully resolved soon.  
    • [SOLVED]  public class RenderGUIHandler { @SubscribeEvent public void renderGUI(RenderGameOverlayEvent.Text event){ Client.hud.draw(); } } As I was playing around a little with the code, i found out about an option to change The RenderGameOverlayEvent.Post to RenderGameOverlayEvent.Text
    • public class HUD { public Minecraft mc = Minecraft.getMinecraft(); public static class ModuleComparator implements Comparator<Module>{ @Override public int compare(Module o1, Module o2) { if (Minecraft.getMinecraft().fontRendererObj.getStringWidth(o1.name) > Minecraft.getMinecraft().fontRendererObj.getStringWidth(o2.name)){ return -1; } if (Minecraft.getMinecraft().fontRendererObj.getStringWidth(o1.name) < Minecraft.getMinecraft().fontRendererObj.getStringWidth(o2.name)){ return 1; } return 0; } } public void draw(){ ScaledResolution sr = new ScaledResolution(mc); FontRenderer fr = mc.fontRendererObj; Collections.sort(Client.modules, new ModuleComparator()); GlStateManager.pushMatrix(); GlStateManager.translate(4,4,0); GlStateManager.scale(1.5,1.5,1); GlStateManager.translate(-4, -4, 0); fr.drawString("Skyline", 10, 10, -1); GlStateManager.popMatrix(); int count = 0; for (Module m : Client.modules){ if (!m.toggled || m.name.equals("TabGUI")) continue; int offset = count* (fr.FONT_HEIGHT + 6); GL11.glTranslated(0.0f, 0.0f, -1.0f); Gui.drawRect(sr.getScaledWidth() - fr.getStringWidth(m.name) - 10, offset, sr.getScaledWidth() - fr.getStringWidth(m.name) - 8, 6 + fr.FONT_HEIGHT + offset, -1); Gui.drawRect(sr.getScaledWidth() - fr.getStringWidth(m.name) - 8, offset, sr.getScaledWidth(), 6 + fr.FONT_HEIGHT + offset, 0x90000000); fr.drawString(m.name, sr.getScaledWidth() - fr.getStringWidth(m.name) - 4, offset + 4, -1); count++; } Client.onEvent(new EventRenderGUI()); } } I have just recently stumbled upon this Problem, where the HudRenderer renders the wrong section of the textures and therefore completely destroys the Minecraft Armour and Hunger Bar. I am currently thinking if it is an issue with the DrawRect changing something it shouldn't. But I couldn't find anything about it. (To keep things Clean, the function is Called from another file) public class RenderGUIHandler { @SubscribeEvent public void renderGUI(RenderGameOverlayEvent.Post event){ Client.hud.draw(); } } Any help would be greatly appreciated  
  • Topics

×
×
  • Create New...

Important Information

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