Custom game rules

Curious if this is possible or if it will be in the future. I tried it but if I recall it threw a UnsupportedOperationException

Details, sir.

Details.


package TopSuggestion;


import com.google.common.collect.Maps;
import com.google.inject.Inject;
import org.spongepowered.api.Sponge;
import org.spongepowered.api.command.CommandResult;
import org.spongepowered.api.command.CommandSource;
import org.spongepowered.api.command.args.GenericArguments;
import org.spongepowered.api.command.spec.CommandSpec;
import org.spongepowered.api.entity.living.player.Player;
import org.spongepowered.api.event.Listener;
import org.spongepowered.api.event.game.state.GameInitializationEvent;
import org.spongepowered.api.plugin.Plugin;
import org.spongepowered.api.text.Texts;
import org.spongepowered.api.text.chat.ChatType;
import org.spongepowered.api.text.chat.ChatTypes;
import org.spongepowered.api.world.World;
import org.spongepowered.api.world.storage.WorldProperties;

import java.util.Map;
import java.util.Optional;
import java.util.logging.Logger;

@Plugin(id = "ForumCommands", name = "ForumCommands", version = "0.1-SNAPSHOT")
public class ForumCommands {

    @Inject
    Logger logger;

    @Listener
    public void onAttackBaby(GameInitializationEvent event) {
        CommandSpec command = CommandSpec.builder()
                .arguments(
                        GenericArguments.playerOrSource(Texts.of("target"), Sponge.getGame()) ,
                        GenericArguments.string(Texts.of("gamerule")),
                        GenericArguments.optional(GenericArguments.string(Texts.of("value")))

                )
                .executor((executor,context)->{
                    Optional<CommandSource> optSource = executor.getCommandSource();
                    Optional<Player> optPlayer = context.getOne("target");
                    Optional<String> optGamerule = context.getOne("gamerule");
                    Optional<String> optValue = context.getOne("value");
                    if(!optPlayer.isPresent()) return CommandResult.empty();
                    if(!optGamerule.isPresent()) return CommandResult.empty();
                    Player player = optPlayer.get();
                    String gamerule = optGamerule.get();
                    WorldProperties properties = player.getLocation().getExtent().getProperties();
                    if(optValue.isPresent()){
                        String value = optValue.get();
                        properties.setGameRule(gamerule, value);
                        player.sendMessage(ChatTypes.SYSTEM, Texts.of("Gamerule set: "+gamerule+" = "+value));
                    } else {
                        Optional<String> x = properties.getGameRule(gamerule);
                        player.sendMessage(ChatTypes.SYSTEM, Texts.of(gamerule + " = " + x.orElse("Not Set")));
                    }
                    return CommandResult.success();
                })
                .build();
        Sponge.getGame().getCommandManager().register(this, command, "testCommand");
    }
}

Never mind. Same as last topic, worked on server but not on client

Works on client too from my testing.

world.getGameRule("keepInventory") --> Optional[false]
world.getProperties().setGameRule("keepInventory", "true");
world.getGameRule("keepInventory") --> Optional[true]
world.getProperties().setGameRule("keepInventory", "false");
world.getGameRule("keepInventory") --> Optional[false]

I only ever test in singleplayer…

It was a couple versions ago when it gave me grief. Let’s just call this topic closed.

1 Like