Jump to content

Reset all player stat when servers start


3mptysl

Recommended Posts

Im using this but doesnt work

@SubscribeEvent
    public static void serverStart(ServerStartedEvent event) {
        MinecraftServer server = event.getServer();
        Stat<ResourceLocation> stat = Stats.CUSTOM.get(StatsInit.MY_PLAY_TIME.get());
        server.getPlayerList().getPlayers().forEach(serverPlayer -> serverPlayer.resetStat(stat));
    }

 

Link to comment
Share on other sites

I need to ban a person when they have played 6 hours in a day and when the day is over, reset the time played so that they can play again. I have done it through a stats timeplayed when it reaches 6h it bans you and resets your time played but I don't know how to make it unban you when the day is over and also if you haven't been banned I reset your stat

Link to comment
Share on other sites

Subscribe to ServerTickEvent, in the Event you need to check the Time, you can get it from LocalTime#now.
Then check if the hour is 0 and if the second is 0, if this is the case clear the banned Players.

Note 1: Checking the second could lead problems if the server is laggy.
Note 2: You should not forget to reset the played time of all Players.

Link to comment
Share on other sites

To answer the original question.

Try something like this **untested** code in your server starting event:

        MinecraftServer server = event.getServer();
        File statsDir = server.getWorldPath(LevelResource.PLAYER_STATS_DIR).toFile();
        
        // Needs an access transformer
        PlayerDataStorage storage = server.playerDataStorage;

        // For each player with an entry in the playerdata folder
        String[] uuids = storage.getSeenPlayers();
        for (String uuid : uuids) {
            
            // Load, change and save the stats
            File playerStats = new File(statsDir, uuid + ".json");
            ServerStatsCounter stats = new ServerStatsCounter(server, playerStats);
            // YOUR CODE HERE
            stats.save();
        }

As it says, you will need an access transformer to make the playerDataStorage field in MinecraftServer public.

https://forge.gemwire.uk/wiki/Access_Transformers

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

I don't know how to use accesstransformers, I have put this in my accesstransformer.cfg:

public net.minecraft.world.level.storage.PlayerDataStorage <init>(Lnet/minecraft/world/level/storage/LevelStorageSource$LevelStorageAccess;Lcom/mojang/datafixers/DataFixer;)V # PlayerDataStorage

but I still don't know what I should do since the server.playerDataStorage doesn't exist idk. what i need to put in accesstranformer.cfg? and how do i get playerDataStorage?

Link to comment
Share on other sites

You changed the **constructor** of PlayerDataStorage to public.

You want to change the playerDataStorage field of MinecraftServer.

 

The link I posted above shows an example of changing a field:

Quote

# Makes protected the 'COLOR_WHITE' field in Gui

protected net.minecraft.client.gui.Gui f_168667_ # COLOR_WHITE

So you need know what forge calls your field

 

There is probably a more efficient way to do this, but this is how I do it:

1) Look at Mojang's deobfuscation mapping to see what they call field:

https://piston-data.mojang.com/v1/objects/8e8c9be5dc27802caba47053d4fdea328f7f89bd/client.txt

With some searching you will find:

Quote

net.minecraft.server.MinecraftServer -> net.minecraft.server.MinecraftServer: 

 

     net.minecraft.world.level.storage.PlayerDataStorage playerDataStorage -> i

This tells you Mojang call the field "i"

 

Now you need to find out what forge calls it:

https://raw.githubusercontent.com/MinecraftForge/MCPConfig/master/versions/release/1.19.2/joined.tsrg

and again some searching is required

Quote

net/minecraft/server/MinecraftServer net/minecraft/src/C_4977_ 4977

 

    i f_129745_ 129745

 

Now you have all the information you need:

public net.minecraft.server.MinecraftServer f_129745_ # playerDataStorage

Double check my logic is correct, I haven't tested this.

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:

There is probably a more efficient way to do this, but this is how I do it:

You should use the Forge Bot on the Forge Discord server.
Command:

!moj <class_name>.<field_name> <version> // The version is optional if you use the latest version (currently 1.19.2)

Use the command in #bot-commands channel

Link to comment
Share on other sites

No, you don't create the player, that will cause all kinds of issues.

You update the ServerStatsCounter directly. e.g. the equivalent of your player.resetStat(stat) is

stats.setValue(null, stat, 0);

Although setValue() takes a player as the first parameter, you can see in the code it doesn't actually use it. So you can pass "null".

 

The idea of using the ServerStatsCounter to update the value is so you don't have worry about the correct handling of the json format.

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

Is that a serious question? Did you even look at that class?

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

Yes idk. i look TickEvent.ServerTickEvent class and i didnt see how i take MinecraftServer, because if i use WorldTickEvent i can take server with event.world.getServer()

maybe i'm tired and i don't see it.

Edited by 3mptysl
Link to comment
Share on other sites

Quote

I suspect the @warjortthought that you are using 1.19 since in 1.19 there is ServerTickEvent#getServer.

Correct.

It was also frustration at having to "spoon feed" every single line of code to the original poster.

I am not sure how much of this feature they have actually written for themselves. 🙂

 

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

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.