Plagiatus / datapacks

A dumping ground for all my Datapacks
https://plagiatus.net/datapacks/
Other
76 stars 16 forks source link

(multiplayer sleep) Option to round up instead. #8

Open technologicalMayhem opened 5 years ago

technologicalMayhem commented 5 years ago

Would it be possible to create an option or a version of the data pack where the amount of players required to sleep is rounded up instead of down? I tried to make the change myself, but i have a hard time wrapping my head around how data packs work, so pointing me towards the point where the rounding actually takes place, would be appreciated as well.

Plagiatus commented 5 years ago

it is calculated in the fittingly named calculate.mcfunction function. and the rounding down comes from integer division rounding down by default (10/3 is 3.33, so 3). see line 11 in that file.

you could of course always just add 1 to required players after line 11, but that means that you'd also "round up" if there is nothing to round (2 players with 100% would then need 3 players).

So, to enable you to round up, you'd have to check whether the division by 100 would turn out with a rest after the division. i threw this thing together real quick and didn't test it (EDIT: I tested it and it works just as expected), you can replace the calculate function file contents with this and see if that then rounds up properly:

execute store result score @s multSleep.tmp if entity @a[nbt={Dimension:0}]
scoreboard players operation totalPlayers multSleep.count = @s multSleep.tmp

execute store result score @s multSleep.tmp if entity @a[scores={multSleep.count=99..}]
scoreboard players operation sleepingPlayers multSleep.count = @s multSleep.tmp

scoreboard players operation requiredPlayers multSleep.count = totalPlayers multSleep.count

scoreboard players operation #roundup multSleep.tmp = totalPlayers multSleep.count
scoreboard players operation #roundup multSleep.tmp *= requiredPercent multSleep.count
scoreboard players operation #roundup multSleep.tmp %= #100 multSleep.count

scoreboard players operation requiredPlayers multSleep.count *= requiredPercent multSleep.count
scoreboard players operation requiredPlayers multSleep.count /= #100 multSleep.count
execute if score #roundup multSleep.tmp matches 1.. run scoreboard players add requiredPlayers multSleep.count 1
scoreboard players operation requiredPlayers multSleep.count -= sleepingPlayers multSleep.count

scoreboard players operation @s multSleep.tmp = requiredPlayers multSleep.count
FredrikM97 commented 4 years ago

Tried this for minecraft 1.15 and it does not seem to be working.

Plagiatus commented 4 years ago

@FredrikM97 sorry for the late reply, but what part of this isn't working? I'll need some more info than just "it isn't working".

Are you using the roundup code I provided or are you referring to something else? Does it not produce the correct amount of people that it should? And if so, too little or too many? Does it break entirely and doesn't produce anything anymore?

FredrikM97 commented 4 years ago

Tried to use the round up code but the datapack stops working (probobly being disabled by the server) in minecraft 1.15. Could potentially be a crash?

Plagiatus commented 4 years ago

no, the server doesn't just disable a datapack, at worst it disables a faulty function. Are you on a vanilla server or a third party one (spigot/bukkit/paper/etc)?

because I just did some testing and it works exactly as expected for me on a vanilla server in 1.15.2