Hi all,
I’m proficient with java and love minecraft, and thought that I might start looking into making plugins. It is cool, except I am not too sure on some of the minecraft interactions.
The ones that I am particularly stuck on are:
How to assign a player input (from server chat) to a variable.
How to display variables in outputs to server chat.
I’m a bit confused about exactly what you want. Can you be more specific?
Is it something like this?
private Text myVariable;
@Listener
public void onChat(MessageChannelEvent.Chat event) {
if(<your check here>) {
myVariable = event.getMessage();
}
}
public void printVariable(MessageChannel channel) {
channel.send(myVariable);
}
The first method is a listener. It’s called whenever some specific thing happens. In this case that thing is that someone chats.
The second method takes a MessageChannel and sends the variable to that channel. A MessageChannel can be though of as a collection of MessageRecievers. A MessageReciever as it’s name implies is something that can receive messages.
@DankMemes This is entirely untested code, but feel to learn from it and/or tweak it to your needs.
import com.google.common.collect.Maps;
import com.google.inject.Inject;
import org.spongepowered.api.Game;
import org.spongepowered.api.command.CommandResult;
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.text.Text;
import javax.annotation.Nonnull;
import java.util.Map;
import java.util.Optional;
import java.util.UUID;
public class Test {
@Nonnull private final Game game;
@Nonnull private final Map<UUID, Map<String, String>> playerVariables;
@Inject public Test(@Nonnull final Game game) {
this.game = game;
this.playerVariables = Maps.newHashMap();
}
@Listener public void onInit(@Nonnull final GameInitializationEvent event) {
final CommandSpec keySetCommand = CommandSpec.builder()
.description(Text.of("Set a value to a key!"))
.permission("test.key.set")
.arguments(
GenericArguments.onlyOne(GenericArguments.string(Text.of("key"))),
GenericArguments.remainingJoinedStrings(Text.of("value")))
.executor((src, args) -> {
if (!(src instanceof Player)) {
src.sendMessage(Text.of("Only a player can assign a variable to themselves!"));
return CommandResult.success();
}
final Optional<String> key = args.getOne("key");
final Optional<String> value = args.getOne("value");
if (!key.isPresent() || !value.isPresent()) {
src.sendMessage(Text.of("This command requires both a key and a value!: /set <key> <value>"));
return CommandResult.success();
}
final UUID uuid = ((Player) src).getUniqueId();
if (!this.playerVariables.containsKey(uuid)) {
this.playerVariables.put(uuid, Maps.newHashMap());
}
this.playerVariables.get(uuid).put(key.get(), value.get()); // Check is already performed above
return CommandResult.success();
})
.build();
final CommandSpec startKeyValue = CommandSpec.builder()
.description(Text.of("Display value to everyone on the server!"))
.permission("test.key.start")
.arguments(GenericArguments.onlyOne(GenericArguments.string(Text.of("key"))))
.executor((src, args) -> {
if (!(src instanceof Player)) {
src.sendMessage(Text.of("Only a player can send out a message!"));
return CommandResult.success();
}
final Optional<String> key = args.getOne("key");
if (!key.isPresent()) {
src.sendMessage(Text.of("This command requires a key! /start <key>"));
return CommandResult.success();
}
final UUID uuid = ((Player) src).getUniqueId();
if (this.playerVariables.get(uuid) == null) {
src.sendMessage(Text.of("You must first create a value for this key! /set <key> <value>"));
return CommandResult.success();
}
final String value = this.playerVariables.get(uuid).get(key.get());
if (value == null) {
src.sendMessage(Text.of("You must first create a value for this key! /set <key> <value>"));
return CommandResult.success();
}
for (final Player player : this.game.getServer().getOnlinePlayers()) {
player.sendMessage(Text.of(src.getName() + "has started " + value));
}
return CommandResult.success();
})
.build();
this.game.getCommandManager().register(this, keySetCommand, "set");
this.game.getCommandManager().register(this, startKeyValue, "start");
}
}