Jump to content

[1.20.4] starting a world for the first time event, to give a player a briefcase whit items once


Recommended Posts

Good days 
advancing whit the problem from a post a made short ago and dint find anyway

anyway i want to give the player a briefcase whit random stuff when play a new world for the first time 

stonehead.png


i made this 

Spoiler

				
			@SubscribeEvent
		public static void WorldInit(ServerStartedEvent event) {
		System.out.println("\n\n\n###WorldInit### " + event.getServer().getWorldData().getLevelName().toString() );
		Level warudo = event.getServer().overworld();
		
		warudo.setBlock( warudo.getSharedSpawnPos().above(), Blocks.GLOWSTONE.defaultBlockState() , 2 );
		
		System.out.println(" warudo " + warudo.isClientSide() + ", => " + warudo.getSharedSpawnPos().above() );
		{
		List<? extends Player> lista = warudo.players();
		System.out.println(" lista " + lista.size() );
		
		for( Player spe : lista ){
		//CompoundTag NBT = spe.serializeNBT();
		//System.out.println( NbtUtils.prettyPrint( NBT ) );
		}
		}
		System.out.println("###WorldInit###\n\n\n");
		}
		
		
		@SubscribeEvent
		public static void WorldStop(ServerStoppedEvent event) {
		System.out.println("\n\n\n###WorldStop###\n\n\n");
		}				
			

this event triggers once the world map finish loading but before the players and the entities get loaded into the world an from it i can get a block position close to the player random spawn point 

and do it every time yo play the world

this block position returned is always the same and is no the same as the bonus chest 

2024-03-24-08-26-04.png

###WorldInit### New World
 warudo false, => BlockPos{x=0, y=54, z=0}
 lista 0 //<-- no players yet 
###WorldInit###

 

- first problem there's no player yet when triggered 

- second one it triggers every time you star the game and loads the world so it keeps creating again the glow stone block those giving the briefcase again 

##############

i was thinking into also spawn a black wool block at the block position but at y -63 soo every time its fires check if its a black wool block at -63 this way knows if its the first time or not 
the other think would be not create directly the briefcase but some block whit logic to tick() 5 seconds later when i suppose the players  already loaded and then create the briefcase in front of the player 

 

########## ########## ########## ########## 

someone knows about another event that trigger only once when the world is loaded and the players already inside the world ? 

way i dont have to made complicated stuff like this to make something like that 

 

 

thanks for your attention 

 

 

 

 

 

 

Link to comment
Share on other sites

So 2 things:

1. You'll need some way to check if it's the first time the Player has logged into the server. You make a custom Player capability that gets saved only server-side that keeps track of if this is the Player's first time on the server. This is Forge's docs on Capabilities: https://docs.minecraftforge.net/en/1.20.x/datastorage/capabilities/

Kind of complicated at first but pretty much every mod is going to need their own Capability, so it'd be good to get familiar sooner rather than later.

2.

Use PlayerLoggedInEvent instead

@SubscribeEvent
public static void onPlayerLoggedIn(PlayerEvent.PlayerLoggedInEvent event) {
	final var player = (ServerPlayer) event.getEntity();
    final var serverPlayerCap = Capabilities.serverPlayer(player);
    if(serverPlayerCap.isFirstLogin()) {
    	player.getInventory().add(new ItemStack(Items.YOUR_ITEM_HERE.get(), 1));
    }
}

 

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.



×
×
  • Create New...

Important Information

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