Jump to content

Get player's dimension


Roland

Recommended Posts

Hello. I'm writing anti-griefing mod. I'm trying to find out in what kind of dimension player currently is. My code sample (just to show that I have ServerPlayer instance and nothing more):

public void onServerTick(TickEvent.ServerTickEvent event) throws SQLException {
    List<ServerPlayer> pl = ServerLifecycleHooks.getCurrentServer().getPlayerList().getPlayers();
    for (ServerPlayer player : pl) {
        // How to get player's dimension??? player.getDimension() not exist

MC version is 1.19.2. Help, please.

Edited by Roland
Link to comment
Share on other sites

This is like minecraft 101.

player.level

Or if you really want what mojang calls the dimension (its registry name)

player.level.dimension()

e.g. Level.OVERWORLD

 

Also,

* If you throw SQLException from that event it will crash the game

* You should check event.haveTime() to see if the server is running slow before doing intense processing (like querying an SQL database)

* There is event.getServer() in 1.19

* All TickEvents are called twice per tick. You need to check event.phase (start or end) to avoid doing it twice per tick

Boilerplate:

If you don't post your logs/debug.log we can't help you. For curseforge you need to enable the forge debug.log in its minecraft settings. You should also post your crash report if you have one.

If there is no error in the log file and you don't have a crash report then post the launcher_log.txt from the minecraft folder. Again for curseforge this will be in your curseforge/minecraft/Install

Large files should be posted to a file sharing site like https://gist.github.com  You should also read the support forum sticky post.

Link to comment
Share on other sites

3 hours ago, warjort said:

This is like minecraft 101.

player.level

Or if you really want what mojang calls the dimension (its registry name)

player.level.dimension()

e.g. Level.OVERWORLD

Thank you! But I need integer representation of current dimention. Because I want to put it to database as integer. BTW, I have tried the following with not result. I miss something simple, because I have no idea what are "ResourceKey", "ResourceLocation" and so on. And why they are implemented.

LOGGER.info(String.valueOf(player.level.dimension()));
// Log:
// ResourceKey[minecraft:dimension / minecraft:overworld]

LOGGER.info(String.valueOf(Level.OVERWORLD));
// Log:
// ResourceKey[minecraft:dimension / minecraft:overworld]

LOGGER.info(String.valueOf(player.level.dimensionTypeId()));
// Log:
// ResourceKey[minecraft:dimension_type / minecraft:overworld]

LOGGER.info(String.valueOf(player.level.dimensionType()));
// Log:
// DimensionType[fixedTime=OptionalLong.empty, hasSkyLight=true, hasCeiling=false, ultraWarm=false, natural=true, coordinateScale=1.0, bedWorks=true, respawnAnchorWorks=false, minY=-64, height=384, logicalHeight=384, infiniburn=TagKey[minecraft:block / minecraft:infiniburn_overworld], effectsLocation=minecraft:overworld, ambientLight=0.0, monsterSettings=MonsterSettings[piglinSafe=false, hasRaids=true, monsterSpawnLightTest=[0-7], monsterSpawnBlockLightLimit=0]]

LOGGER.info(String.valueOf(player.level.dimension() == Level.OVERWORLD));
// Log:
// true

Added later: My guess is that the dimension is stored as an enum somewhere. And so I can get its numeric representation. Seems I'm wrong...

3 hours ago, warjort said:

* If you throw SQLException from that event it will crash the game

Yes, I know. Already, haha. It's okay for now.

3 hours ago, warjort said:

* You should check event.haveTime() to see if the server is running slow before doing intense processing (like querying an SQL database)

Very interesting! But if tick is gone it will never repeat... And if I want to write some kind of logger... Well. Do I need to create new threads with SQL-insert-queries? The reason is: I _need_ to write some data to database exactly when it happened and never-never-never skip writing operations. I think MineCraft hasn't things like queues?

3 hours ago, warjort said:

* There is event.getServer() in 1.19

Thank you! I'm new in modding. Any information is useful. Does Forge community has some guides may be? I google every time I need something. It's not the best way to learn something new. And to code correctly.

3 hours ago, warjort said:

* All TickEvents are called twice per tick. You need to check event.phase (start or end) to avoid doing it twice per tick

Oh, thank you, sir! That's why I wondering how it work twice a time... I'll fix it right now.

Edited by Roland
Link to comment
Share on other sites

There hasn't been a numeric representation of dimensions since 1.12

Boilerplate:

If you don't post your logs/debug.log we can't help you. For curseforge you need to enable the forge debug.log in its minecraft settings. You should also post your crash report if you have one.

If there is no error in the log file and you don't have a crash report then post the launcher_log.txt from the minecraft folder. Again for curseforge this will be in your curseforge/minecraft/Install

Large files should be posted to a file sharing site like https://gist.github.com  You should also read the support forum sticky post.

Link to comment
Share on other sites

8 minutes ago, warjort said:

There hasn't been a numeric representation of dimensions since 1.12

Sorry for wasting your time. My last question (in this thread, haha). 

Is that okay approach for Minecraft mods? It this approach okay for mod development?

// Somewhere in main Mod class (marked as @Mod):
(new LogThread()).start();
// Thread class (example with SQL insert query):
public class LogThread extends Thread {
    public void run() {
        try {
            db.createStatement().executeUpdate("INSERT INTO ...");
        } catch (Exception e) {
            // ...
        }
    }
}

 

Link to comment
Share on other sites

You can start a new thread If you know what you are doing.

But Minecraft is not designed to be used concurrently, except in a very few specific places. There is only one main client and server thread..

If you start using datastructures that are not designed to be accessed concurrently you are at best going to see inconsistent data,

at worst you will get exceptions (NPEs, ConcurrentModification on iterators, etc.) due to incorrect concurrent access.

e.g. https://bugs.mojang.com/browse/MC-258939

Edited by warjort

Boilerplate:

If you don't post your logs/debug.log we can't help you. For curseforge you need to enable the forge debug.log in its minecraft settings. You should also post your crash report if you have one.

If there is no error in the log file and you don't have a crash report then post the launcher_log.txt from the minecraft folder. Again for curseforge this will be in your curseforge/minecraft/Install

Large files should be posted to a file sharing site like https://gist.github.com  You should also read the support forum sticky post.

Link to comment
Share on other sites

1 hour ago, warjort said:

You can start a new thread If you know what you are doing.

But Minecraft is not designed to be used concurrently, except in a very few specific places. There is only one main client and server thread..

If you start using datastructures that are not designed to be accessed concurrently you are at best going to see inconsistent data,

at worst you will get exceptions (NPEs, ConcurrentModification on iterators, etc.) due to incorrect concurrent access.

e.g. https://bugs.mojang.com/browse/MC-258939

I understand. There is no need in any state for logger for now. Thank you for answering!

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.