Jump to content

Recommended Posts

Posted

Hi, I made a block that on right click executes a random action. I want In all of them to give an Item to the player that clicked the block. This Items are from other mod so i think the best way would be giving it by ID.

 

Thanks

Twitter: @deantonious || TeamSpeak Server: ts3.deantonious.es

Posted

If it's a block, using onActivated from the block class should do the trick, just throw in

 

player.inventory.addItemStackToInventory(ItemStack);

 

Where player is your EntityPlayer.

 

As for the ID, that's up to you ;)

Posted

I've got this

((EntityPlayer)MinecraftServer.getServer().getConfigurationManager().playerEntityList.get(0)).inventory.addItemStackToInventory(new ItemStack(ModPowderCraft.explorerKit));

 

But I cant use the item ID... I've already look into the CommandGive without getting any idea of how to do it...

Twitter: @deantonious || TeamSpeak Server: ts3.deantonious.es

Posted

My bad, the /give command uses an ItemStack.

 

You could get the item itself by using the findItem method in GameRegistry. All you need is the mod's ID and the item's name.

 

Item item = GameRegistry.findItem("ic2", "copperIngot");

Kain

Posted

Sorry, I havnt done much in the way of tinkering with other peoples mods. My only help would be, try putting a "mod_" before the modID, to see if that helps

Posted

I tried the mod in my server and I found an error:

It gives you the item, but only in Client Side... When I right click on the inventory, all the items that the mod gave me disapears... How I enable this to be in server side?

package deantonious.blocks;

import java.util.ArrayList;
import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;

import cpw.mods.fml.common.registry.GameRegistry;
import cpw.mods.fml.relauncher.SideOnly;

import deantonious.ModPowderCraft;
import net.minecraft.block.material.Material;
import net.minecraft.block.Block;
import net.minecraft.entity.item.EntityItem;
import net.minecraft.entity.monster.EntityCreeper;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.server.MinecraftServer;
import net.minecraft.src.ModLoader;
import net.minecraft.world.World;
import net.minecraftforge.client.MinecraftForgeClient;
import net.minecraftforge.common.MinecraftForge;

public class BlockMenaSubsuelo extends Block 
{

        public BlockMenaSubsuelo(int id, Material material) 
        {
                super(id, material);
        }
        
        @Override
    	public boolean onBlockActivated(World par1World, int par2, int par3, int par4, EntityPlayer par5EntityPlayer, int par6, float par7, float par8, float par9)
    	{

            if(par5EntityPlayer.getHeldItem() == null)
    		{
            	if(par1World.isRemote){
            		ModLoader.getMinecraftInstance().thePlayer.addChatMessage("Necesitas un Cincel");
        			
            	}
            	return super.onBlockActivated(par1World, par2, par3, par4, par5EntityPlayer, par6, par7, par8, par9);
    		}

    		if(par5EntityPlayer.getHeldItem().itemID == ModPowderCraft.cincel.itemID)
    		{

    			par5EntityPlayer.getHeldItem().stackSize -= 1; //only if you want to use the item when you do this.
    			if(par1World.isRemote)
    			{
    				par1World.playSound((double)par2 + 0.5D, (double)par3 + 0.1D, (double)par4 + 0.5D, "random.break", 0.3F, 0.5F, blockConstructorCalled);
    				par1World.playSound((double)par2 + 0.5D, (double)par3 + 0.1D, (double)par4 + 0.5D, "random.orb", 0.3F, 0.5F, blockConstructorCalled);
    				
    				int rand = par1World.rand.nextInt(60);

    	                if(rand<=8 && rand>=0){
    	                	ModLoader.getMinecraftInstance().thePlayer.addChatMessage("Has encontrado una Gema Azul");
    	                	par5EntityPlayer.inventory.addItemStackToInventory(new ItemStack(ModPowderCraft.gemaazul));
        	                
    	                }
    	                else if(rand<=16 && rand>={
    	                	ModLoader.getMinecraftInstance().thePlayer.addChatMessage("Has encontrado una Gema Roja");
    	                	par5EntityPlayer.inventory.addItemStackToInventory(new ItemStack(ModPowderCraft.gemaroja));
        	                
    	                }
    	                else if(rand<=24 && rand>=16){
    	                	ModLoader.getMinecraftInstance().thePlayer.addChatMessage("Has encontrado una Gema Verde");
    	                	par5EntityPlayer.inventory.addItemStackToInventory(new ItemStack(ModPowderCraft.gemaverde));
        	                
    	                }
    	                else if(rand<=32 && rand>=24){
    	                	ModLoader.getMinecraftInstance().thePlayer.addChatMessage("Has encontrado una Gema Blanca");
    	                	par5EntityPlayer.inventory.addItemStackToInventory(new ItemStack(ModPowderCraft.gemablanca));
        	                
    	                }
    	                else if(rand<=40 && rand>=32){
    	                	ModLoader.getMinecraftInstance().thePlayer.addChatMessage("Has encontrado una Gema Prisma");
    	                	par5EntityPlayer.inventory.addItemStackToInventory(new ItemStack(ModPowderCraft.gemaprisma));
        	                
    	                }
    	                else if(rand<=42 && rand>=40){
    	                	ModLoader.getMinecraftInstance().thePlayer.addChatMessage("Has encontrado un Fosil Cubierto");
    	                	par5EntityPlayer.inventory.addItemStackToInventory(new ItemStack(ModPowderCraft.ArmorFossilCovered));
    	                
    	                }
    	                else if(rand<=44 && rand>=42){
    	                	ModLoader.getMinecraftInstance().thePlayer.addChatMessage("Has encontrado un Fosil Cubierto");
    	                	par5EntityPlayer.inventory.addItemStackToInventory(new ItemStack(ModPowderCraft.ClawFossilCovered));
    	                }
    	                else if(rand<=46 && rand>=44){
    	                	ModLoader.getMinecraftInstance().thePlayer.addChatMessage("Has encontrado un Fosil Cubierto");
    	                	par5EntityPlayer.inventory.addItemStackToInventory(new ItemStack(ModPowderCraft.CoverFossilCovered));
    	                }
    	                else if(rand<=48 && rand>=46){
    	                	ModLoader.getMinecraftInstance().thePlayer.addChatMessage("Has encontrado un Fosil Cubierto");
    	                	par5EntityPlayer.inventory.addItemStackToInventory(new ItemStack(ModPowderCraft.DomeFossilCovered));
    	                }
    	                else if(rand<=50 && rand>=48){
    	                	ModLoader.getMinecraftInstance().thePlayer.addChatMessage("Has encontrado un Fosil Cubierto");
    	                	par5EntityPlayer.inventory.addItemStackToInventory(new ItemStack(ModPowderCraft.HelixFossilCovered));
    	                }
    	                else if(rand<=52 && rand>=50){
    	                	ModLoader.getMinecraftInstance().thePlayer.addChatMessage("Has encontrado un Fosil Cubierto");
    	                	par5EntityPlayer.inventory.addItemStackToInventory(new ItemStack(ModPowderCraft.OldAmberCovered));
    	                }
    	                else if(rand<=54 && rand>=52){
    	                	ModLoader.getMinecraftInstance().thePlayer.addChatMessage("Has encontrado un Fosil Cubierto");
    	                	par5EntityPlayer.inventory.addItemStackToInventory(new ItemStack(ModPowderCraft.PlumeFossilCovered));
    	                }
    	              
    	                else if(rand<=56 && rand>=54){
    	                	ModLoader.getMinecraftInstance().thePlayer.addChatMessage("Has encontrado un Fosil Cubierto");
    	                	par5EntityPlayer.inventory.addItemStackToInventory(new ItemStack(ModPowderCraft.RootFossilCovered));
    	                }
    	                else if(rand<=58 && rand>=56){
    	                	ModLoader.getMinecraftInstance().thePlayer.addChatMessage("Has encontrado un Fosil Cubierto");
    	                	par5EntityPlayer.inventory.addItemStackToInventory(new ItemStack(ModPowderCraft.SkullFossilCovered));
    	                }
    	                else if(rand<=60 && rand>=58){
    	                	ModLoader.getMinecraftInstance().thePlayer.addChatMessage("Has encontrado un Hueso");
    	                	par5EntityPlayer.inventory.addItemStackToInventory(new ItemStack(Item.bone));
    	                }          
    	                return true;
    	                

    				
    			}
    			
        		par1World.setBlock(par2, par3, par4, Block.dirt.blockID);
    			return false;
    			    			
    		            
    		
    		}

    		return false;
    	}
          

}

Twitter: @deantonious || TeamSpeak Server: ts3.deantonious.es

Posted

Everything works on single player, but when I play MP it gives the items, messages, etc. and then whe you try to take them, all the items disapear... But that only happens in survival mode, in creative mode it works...

Twitter: @deantonious || TeamSpeak Server: ts3.deantonious.es

Posted

If I don't use the isRemote, It makes the action two times... (Without the isRemote the ploblem dosn't fix)

It's just in survival mode... Remember you are right clicking on a block

Twitter: @deantonious || TeamSpeak Server: ts3.deantonious.es

Posted

Hi

 

What they mean is... you should perform this action on the server side only, not the client side.

 

if(!par1World.isRemote) { 
  // give item code
}

The other bit I suspect might be important is to tell the server to send the updated inventory to the client

 

((EntityPlayerMP)par5EntityPlayer).sendContainerToPlayer(par5EntityPlayer.inventoryContainer);

 

- see BlockCauldron.onBlockActivated

 

-TGG

 

 

 

 

 

Posted

This is the actual code, please tell me what I need to change:

package deantonious.blocks;

import java.util.ArrayList;
import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;

import cpw.mods.fml.common.registry.GameRegistry;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;

import deantonious.ModPowderCraft;
import net.minecraft.block.material.Material;
import net.minecraft.block.Block;
import net.minecraft.entity.item.EntityItem;
import net.minecraft.entity.monster.EntityCreeper;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.server.MinecraftServer;
import net.minecraft.src.ModLoader;
import net.minecraft.world.World;
import net.minecraftforge.client.MinecraftForgeClient;
import net.minecraftforge.common.MinecraftForge;

public class BlockMenaSubsuelo extends Block 
{

        public BlockMenaSubsuelo(int id, Material material) 
        {
                super(id, material);
        }
        
        @Override
    	public boolean onBlockActivated(World par1World, int par2, int par3, int par4, EntityPlayer par5EntityPlayer, int par6, float par7, float par8, float par9)
    	{

            if(par5EntityPlayer.getHeldItem() == null)
    		{
            	if(par1World.isRemote){
            		par5EntityPlayer.addChatMessage("Necesitas un Cincel");
        			
            	}
            	return super.onBlockActivated(par1World, par2, par3, par4, par5EntityPlayer, par6, par7, par8, par9);
    		}

    		if(par5EntityPlayer.getHeldItem().itemID == ModPowderCraft.cincel.itemID)
    		{

    			par5EntityPlayer.getHeldItem().stackSize -= 1; //only if you want to use the item when you do this.
    			if(par1World.isRemote)
    			{
    				par1World.playSound((double)par2 + 0.5D, (double)par3 + 0.1D, (double)par4 + 0.5D, "random.break", 0.3F, 0.5F, blockConstructorCalled);
    				par1World.playSound((double)par2 + 0.5D, (double)par3 + 0.1D, (double)par4 + 0.5D, "random.orb", 0.3F, 0.5F, blockConstructorCalled);
    				
    				int rand = par1World.rand.nextInt(60);

    	                if(rand<=8 && rand>=0){
    	                	par5EntityPlayer.addChatMessage("Has encontrado una Gema Azul");
    	                	par5EntityPlayer.inventory.addItemStackToInventory(new ItemStack(ModPowderCraft.gemaazul));
        	                
    	                }
    	                else if(rand<=16 && rand>={
    	                	par5EntityPlayer.addChatMessage("Has encontrado una Gema Roja");
    	                	par5EntityPlayer.inventory.addItemStackToInventory(new ItemStack(ModPowderCraft.gemaroja));
        	                
    	                }
    	                else if(rand<=24 && rand>=16){
    	                	par5EntityPlayer.addChatMessage("Has encontrado una Gema Verde");
    	                	par5EntityPlayer.inventory.addItemStackToInventory(new ItemStack(ModPowderCraft.gemaverde));
        	                
    	                }
    	                else if(rand<=32 && rand>=24){
    	                	par5EntityPlayer.addChatMessage("Has encontrado una Gema Blanca");
    	                	par5EntityPlayer.inventory.addItemStackToInventory(new ItemStack(ModPowderCraft.gemablanca));
        	                
    	                }
    	                else if(rand<=40 && rand>=32){
    	                	par5EntityPlayer.addChatMessage("Has encontrado una Gema Prisma");
    	                	par5EntityPlayer.inventory.addItemStackToInventory(new ItemStack(ModPowderCraft.gemaprisma));
        	                
    	                }
    	                else if(rand<=42 && rand>=40){
    	                	par5EntityPlayer.addChatMessage("Has encontrado un Fosil Cubierto");
    	                	par5EntityPlayer.inventory.addItemStackToInventory(new ItemStack(ModPowderCraft.ArmorFossilCovered));
    	                
    	                }
    	                else if(rand<=44 && rand>=42){
    	                	par5EntityPlayer.addChatMessage("Has encontrado un Fosil Cubierto");
    	                	par5EntityPlayer.inventory.addItemStackToInventory(new ItemStack(ModPowderCraft.ClawFossilCovered));
    	                }
    	                else if(rand<=46 && rand>=44){
    	                	par5EntityPlayer.addChatMessage("Has encontrado un Fosil Cubierto");
    	                	par5EntityPlayer.inventory.addItemStackToInventory(new ItemStack(ModPowderCraft.CoverFossilCovered));
    	                }
    	                else if(rand<=48 && rand>=46){
    	                	par5EntityPlayer.addChatMessage("Has encontrado un Fosil Cubierto");
    	                	par5EntityPlayer.inventory.addItemStackToInventory(new ItemStack(ModPowderCraft.DomeFossilCovered));
    	                }
    	                else if(rand<=50 && rand>=48){
    	                	par5EntityPlayer.addChatMessage("Has encontrado un Fosil Cubierto");
    	                	par5EntityPlayer.inventory.addItemStackToInventory(new ItemStack(ModPowderCraft.HelixFossilCovered));
    	                }
    	                else if(rand<=52 && rand>=50){
    	                	par5EntityPlayer.addChatMessage("Has encontrado un Fosil Cubierto");
    	                	par5EntityPlayer.inventory.addItemStackToInventory(new ItemStack(ModPowderCraft.OldAmberCovered));
    	                }
    	                else if(rand<=54 && rand>=52){
    	                	par5EntityPlayer.addChatMessage("Has encontrado un Fosil Cubierto");
    	                	par5EntityPlayer.inventory.addItemStackToInventory(new ItemStack(ModPowderCraft.PlumeFossilCovered));
    	                }
    	              
    	                else if(rand<=56 && rand>=54){
    	                	par5EntityPlayer.addChatMessage("Has encontrado un Fosil Cubierto");
    	                	par5EntityPlayer.inventory.addItemStackToInventory(new ItemStack(ModPowderCraft.RootFossilCovered));
    	                }
    	                else if(rand<=58 && rand>=56){
    	                	par5EntityPlayer.addChatMessage("Has encontrado un Fosil Cubierto");
    	                	par5EntityPlayer.inventory.addItemStackToInventory(new ItemStack(ModPowderCraft.SkullFossilCovered));
    	                }
    	                else if(rand<=60 && rand>=58){
    	                	par5EntityPlayer.addChatMessage("Has encontrado un Hueso");
    	                	par5EntityPlayer.inventory.addItemStackToInventory(new ItemStack(Item.bone));
    	                }          
    	                return true;
    				
    			}
    			
        		par1World.setBlock(par2, par3, par4, Block.dirt.blockID);
    			return false;	            
    		
    		}

    		return false;
    	}
          

}

Twitter: @deantonious || TeamSpeak Server: ts3.deantonious.es

Posted

I want all of them to execute in both server and client. I don't really know what .isRemote is, but without it, it gives two items

Twitter: @deantonious || TeamSpeak Server: ts3.deantonious.es

Posted

When I change it to !isRemote, it gives me the item but its invisible... when I click on it all of them apear... adn also the block doesn't sets to dirt...

Twitter: @deantonious || TeamSpeak Server: ts3.deantonious.es

Posted

Hi

 

I suspect it might be important to tell the server to send the updated inventory to the client

 

((EntityPlayerMP)par5EntityPlayer).sendContainerToPlayer(par5EntityPlayer.inventoryContainer);

 

- see BlockCauldron.onBlockActivated

 

-TGG

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

    • Make a test with another Launcher like the Curseforge Launcher, MultiMC or AT Launcher
    • can anyone help me i am opening forge and add modpacks and then it says unable to update native luancher and i redownlaod java and the luancher it self?
    • The problem occurs also in 1.20.1 Forge, but with an "Error executing task on client" instead. I have "Sinytra Connector" installed. On 1.21.5 Fabric, there is no problem. When this happens, the chat message before the death screen appears gets sent, with an extra dash added.
    • Well, as usual, it was user error. Naming mismatch in sounds.json.  Please delete this post if you find it necessary. 
    • Hello Forge community.  I'm running into an issue with a mod I'm working on.  To preface, I can call /playsound modId:name music @a and I can hear the sound I registered being played in game. Great!  However, I cannot get it to trigger via my mod code.    Registration: public static final RegistryObject<SoundEvent> A_WORLD_OF_MADNESS = SOUND_EVENTS.register("a_world_of_madness", () -> new SoundEvent(new ResourceLocation("tetheredsouls", "a_world_of_madness")));   Playback: Minecraft mc = Minecraft.getInstance(); if (!(mc.player instanceof LocalPlayer) || mc.level == null) return; LocalPlayer player = (LocalPlayer) mc.player; BlockPos pos = player.blockPosition(); SoundEvent track = ModSounds.A_WORLD_OF_MADNESS.get(); System.out.println(track); System.out.println(pos); System.out.println(player); // play exactly like the tutorial: client-only, at the player's position try { mc.level.playLocalSound( player.getX(), player.getY(), player.getZ(), track, SoundSource.MUSIC, // Or MASTER if needed 1f, 1f, false ); System.out.println("[DEBUG] playSound success: " + track.getLocation()); } catch (Exception e) { System.err.println("[ERROR] Failed to play sound: " + track.getLocation()); e.printStackTrace(); } Sounds.json:   { "theme_of_laura": { "category": "music", "sounds": [ { "name": "tetheredsouls:a_world_of_madness", "stream": true } ] } } Things I have tried: - multiple .ogg files. Short .ogg files (5 seconds, <100KB).  - default minecraft sounds imported from import net.minecraft.sounds.SoundEvents; These work given my code. No idea why these are different.  - playSound() method, as well as several others in past iterations that did not work   I would be forever grateful if somebody could point me in the right direction. I've looked at several mod github repositories and found extremely similar code to what I'm doing. I've also found several threads in this forum that did not solve my issue. I just cannot figure out what I'm doing differently, and why I'm able to queue sounds manually with playsound but the code won't play it (despite confirming the code is being run with the debug statements.)
  • Topics

×
×
  • Create New...

Important Information

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