Jump to content

[1.10.2] Message to all players on server


DarkProd02

Recommended Posts

Hi all, 

I try to send a message to all players when a players do right-click on an item.

here is my code: 

package fr.darkprod.archymod.items;

import java.util.List;

import org.lwjgl.input.Keyboard;

import com.mojang.realmsclient.gui.ChatFormatting;
import com.mojang.realmsclient.util.Pair;

import fr.darkprod.archymod.ArchyMod;
import fr.darkprod.archymod.References;
import fr.darkprod.archymod.handlers.ArchydiumSoundHandler;
import fr.darkprod.archymod.init.ModBlocks;
import net.minecraft.block.Block;
import net.minecraft.block.state.IBlockState;
import net.minecraft.client.Minecraft;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.init.Blocks;
import net.minecraft.inventory.ISidedInventory;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.server.MinecraftServer;
import net.minecraft.util.ActionResult;
import net.minecraft.util.EnumActionResult;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.EnumHand;
import net.minecraft.util.SoundCategory;
import net.minecraft.util.SoundEvent;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.text.ITextComponent;
import net.minecraft.util.text.TextComponentString;
import net.minecraft.world.World;
import net.minecraftforge.fml.common.FMLCommonHandler;
import net.minecraftforge.items.IItemHandler;
import net.minecraftforge.items.ItemHandlerHelper;
import scala.util.Random;

public class PremiumLeather extends Item {
	public PremiumLeather(String name) {
		
		this.setRegistryName(name);
		this.setUnlocalizedName(name);
		this.setCreativeTab(ArchyMod.ArchyMod);
	}

	@Override
	public ActionResult<ItemStack> onItemRightClick(ItemStack itemStackIn, World worldIn, EntityPlayer playerIn,EnumHand hand) {
		
		if (playerIn.capabilities.isCreativeMode) {
				
			if (worldIn.isRemote) {
				
			
			Random randomX = new Random();
			int maxX = 200;
			int minX = 1;
			int randomX1 = randomX.nextInt((maxX - minX) + 1);//random number from 1 to 100.
			
			Random randomY = new Random();
			int maxY = 200;
			int minY = 1;
			int randomY1 = randomY.nextInt((maxY - minY) + 1);//random number from 1 to 100.
			
			Random randomZ = new Random();
			int maxZ = 200;
			int minZ = 1;
			int randomZ1 = randomZ.nextInt((maxZ - minZ) + 1);//random number from 1 to 100.
			
			Block block = ModBlocks.ArchydiumBlock;
			BlockPos blockPos = new BlockPos(randomX1,randomY1-1,randomZ1);
			IBlockState blockState = block.getDefaultState();
			worldIn.setBlockState(blockPos,blockState);
			
			/*Block block2 = Blocks.STONE;
			BlockPos blockPos2 = new BlockPos(randomX1,randomY1-2,randomZ1);
			IBlockState blockState2 = block2.getDefaultState();
			worldIn.setBlockState(blockPos2,blockState2);*/
			
			FMLCommonHandler.instance().getMinecraftServerInstance().getServer().getPlayerList().sendChatMsg(new TextComponentString("Test"));
			
			if (playerIn instanceof EntityPlayer) {
				
			}
			worldIn.playSound(playerIn, randomX1, randomY1, randomZ1, ArchydiumSoundHandler.music, SoundCategory.RECORDS, 1.0F, 1.0F);
			
			}else {
				
			}
		}else {
			playerIn.addChatMessage(new TextComponentString(ChatFormatting.RED+"Vous devez etre en mode creatif pour faire un right-click et lancer la musique!"));
		}
		return new ActionResult<ItemStack>(EnumActionResult.SUCCESS, itemStackIn);
	}
	
	@Override
	public void addInformation(ItemStack stack, EntityPlayer playerIn, List<String> tooltip, boolean advanced) {
		
		String ifOpen = ""+ ChatFormatting.GOLD + ChatFormatting.ITALIC + "Right-Click on this item to Play Music ";
		
		if (Keyboard.isKeyDown(Keyboard.KEY_LSHIFT)){
			tooltip.remove(References.HoldShift);
			tooltip.add(ifOpen);
		}else {
			tooltip.remove(ifOpen);
			tooltip.add(References.HoldShift);
		}
	}
}

and here is the error

Quote

---- Minecraft Crash Report ----
// Daisy, daisy...

Time: 12/06/18 18:25
Description: Unexpected error

java.lang.NullPointerException: Unexpected error
    at fr.darkprod.archymod.items.PremiumLeather.onItemRightClick(PremiumLeather.java:80)
    at net.minecraft.item.ItemStack.useItemRightClick(ItemStack.java:179)
    at net.minecraft.client.multiplayer.PlayerControllerMP.processRightClick(PlayerControllerMP.java:527)
    at net.minecraft.client.Minecraft.rightClickMouse(Minecraft.java:1629)
    at net.minecraft.client.Minecraft.processKeyBinds(Minecraft.java:2281)
    at net.minecraft.client.Minecraft.runTickKeyboard(Minecraft.java:2058)
    at net.minecraft.client.Minecraft.runTick(Minecraft.java:1846)
    at net.minecraft.client.Minecraft.runGameLoop(Minecraft.java:1118)
    at net.minecraft.client.Minecraft.run(Minecraft.java:406)
    at net.minecraft.client.main.Main.main(Main.java:118)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at net.minecraft.launchwrapper.Launch.launch(Launch.java:135)
    at net.minecraft.launchwrapper.Launch.main(Launch.java:28)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at net.minecraftforge.gradle.GradleStartCommon.launch(GradleStartCommon.java:97)
    at GradleStart.main(GradleStart.java:26)


A detailed walkthrough of the error, its code path and all known details is as follows:
---------------------------------------------------------------------------------------

-- Head --
Thread: Client thread
Stacktrace:
    at fr.darkprod.archymod.items.PremiumLeather.onItemRightClick(PremiumLeather.java:80)
    at net.minecraft.item.ItemStack.useItemRightClick(ItemStack.java:179)
    at net.minecraft.client.multiplayer.PlayerControllerMP.processRightClick(PlayerControllerMP.java:527)
    at net.minecraft.client.Minecraft.rightClickMouse(Minecraft.java:1629)
    at net.minecraft.client.Minecraft.processKeyBinds(Minecraft.java:2281)
    at net.minecraft.client.Minecraft.runTickKeyboard(Minecraft.java:2058)

-- Affected level --
Details:
    Level name: MpServer
    All players: 1 total; [EntityPlayerSP['Player545'/212, l='MpServer', x=-4,70, y=4,00, z=-4,70]]
    Chunk stats: MultiplayerChunkCache: 441, 441
    Level seed: 0
    Level generator: ID 00 - default, ver 1. Features enabled: false
    Level generator options: 
    Level spawn location: World: (0,63,0), Chunk: (at 0,3,0 in 0,0; contains blocks 0,0,0 to 15,255,15), Region: (0,0; contains chunks 0,0 to 31,31, blocks 0,0,0 to 511,255,511)
    Level time: 14362 game time, 14362 day time
    Level dimension: 0
    Level storage version: 0x00000 - Unknown?
    Level weather: Rain time: 0 (now: false), thunder time: 0 (now: false)
    Level game mode: Game mode: survival (ID 0). Hardcore: false. Cheats: false
    Forced entities: 9 total; [EntityPlayerSP['Player545'/212, l='MpServer', x=-4,70, y=4,00, z=-4,70], EntityItem['item.item.dyePowder.black'/85, l='MpServer', x=-0,22, y=4,00, z=39,78], EntityRabbit['Rabbit'/119, l='MpServer', x=66,50, y=70,00, z=10,50], EntityRabbit['Rabbit'/120, l='MpServer', x=69,32, y=70,00, z=10,56], EntitySkeleton['Skeleton'/329, l='MpServer', x=4,50, y=4,00, z=-4,50], EntitySkeleton['Skeleton'/363, l='MpServer', x=3,50, y=13,00, z=1,50], EntitySkeleton['Skeleton'/286, l='MpServer', x=1,50, y=13,00, z=2,50], EntitySkeleton['Skeleton'/302, l='MpServer', x=4,50, y=12,00, z=0,50], EntityBat['Bat'/350, l='MpServer', x=3,22, y=15,21, z=-0,85]]
    Retry entities: 0 total; []
    Server brand: fml,forge
    Server type: Non-integrated multiplayer server
Stacktrace:
    at net.minecraft.client.multiplayer.WorldClient.addWorldInfoToCrashReport(WorldClient.java:456)
    at net.minecraft.client.Minecraft.addGraphicsAndWorldToCrashReport(Minecraft.java:2779)
    at net.minecraft.client.Minecraft.run(Minecraft.java:435)
    at net.minecraft.client.main.Main.main(Main.java:118)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at net.minecraft.launchwrapper.Launch.launch(Launch.java:135)
    at net.minecraft.launchwrapper.Launch.main(Launch.java:28)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at net.minecraftforge.gradle.GradleStartCommon.launch(GradleStartCommon.java:97)
    at GradleStart.main(GradleStart.java:26)

-- System Details --
Details:
    Minecraft Version: 1.10.2
    Operating System: Windows 10 (amd64) version 10.0
    Java Version: 1.8.0_171, Oracle Corporation
    Java VM Version: Java HotSpot(TM) 64-Bit Server VM (mixed mode), Oracle Corporation
    Memory: 703572312 bytes (670 MB) / 1038876672 bytes (990 MB) up to 2062286848 bytes (1966 MB)
    JVM Flags: 3 total; -Xincgc -Xmx2000M -Xms1024M
    IntCache: cache: 0, tcache: 0, allocated: 13, tallocated: 95
    FML: MCP 9.32 Powered by Forge 12.18.3.2185 5 mods loaded, 5 mods active
    States: 'U' = Unloaded 'L' = Loaded 'C' = Constructed 'H' = Pre-initialized 'I' = Initialized 'J' = Post-initialized 'A' = Available 'D' = Disabled 'E' = Errored
    UCHIJAAAAAA    mcp{9.19} [Minecraft Coder Pack] (minecraft.jar) 
    UCHIJAAAAAA    FML{8.0.99.99} [Forge Mod Loader] (forgeSrc-1.10.2-12.18.3.2185.jar) 
    UCHIJAAAAAA    Forge{12.18.3.2185} [Minecraft Forge] (forgeSrc-1.10.2-12.18.3.2185.jar) 
    UCHIJAAAAAA    cjcore{0.0.3.2} [CJCore] (cjcore-1.10-0.0.3.2.jar) 
    UCHIJAAAAAA    archydium{4.1} [Archymod] (bin) 
    Loaded coremods (and transformers): 
    GL info: ' Vendor: 'Intel' Version: '4.0.0 - Build 10.18.10.4885' Renderer: 'Intel(R) HD Graphics 4000'
    Launched Version: 1.10.2
    LWJGL: 2.9.4
    OpenGL: Intel(R) HD Graphics 4000 GL version 4.0.0 - Build 10.18.10.4885, Intel
    GL Caps: Using GL 1.3 multitexturing.
Using GL 1.3 texture combiners.
Using framebuffer objects because OpenGL 3.0 is supported and separate blending is supported.
Shaders are available because OpenGL 2.1 is supported.
VBOs are available because OpenGL 1.5 is supported.

    Using VBOs: Yes
    Is Modded: Definitely; Client brand changed to 'fml,forge'
    Type: Client (map_client.txt)
    Resource Packs: 
    Current Language: English (US)
    Profiler Position: N/A (disabled)
    CPU: 4x Intel(R) Core(TM) i3-3217U CPU @ 1.80GHz

Thnx :)

Link to comment
Share on other sites

2 minutes ago, DarkProd02 said:

FMLCommonHandler.instance().getMinecraftServerInstance().getServer().getPlayerList().sendChatMsg(new TextComponentString("Test"));

You can't do this on the client.

Edited by Draco18s

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Link to comment
Share on other sites

You need to send a packet to the server and the server sends a message to all players.

 

1 hour ago, DarkProd02 said:

worldIn.setBlockState(blockPos,blockState);

 

Which is true for setting the blockstate, too.

Edited by Draco18s

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Link to comment
Share on other sites

6 minutes ago, diesieben07 said:

No. Look at the context please...

You are correct, that method already runs on the server.

I was being hasty.

Still, it must happen on the server.

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

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



×
×
  • Create New...

Important Information

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