Skip to content

Commit

Permalink
Add "/text playback" to edit playback time
Browse files Browse the repository at this point in the history
  • Loading branch information
SkytAsul committed Sep 24, 2021
1 parent 3a95e52 commit 0f73653
Show file tree
Hide file tree
Showing 7 changed files with 99 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ public boolean onCommand(CommandSender sender, String[] args, OptionName option)
return false;
}
option.setValue(null);
sender.sendMessage("§aCustom name removed. (old : \"" + oldName + "§r§a\")");
sender.sendMessage("§aCustom name removed. (old: \"" + oldName + "§r§a\")");
}else {
option.setValue(String.join(" ", args));
sender.sendMessage("§aCustom name edited. (old : \"" + oldName + "§r§a\")");
sender.sendMessage("§aCustom name edited. (old: \"" + oldName + "§r§a\")");
}
return true;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package fr.skytasul.citizenstext.command;

import org.bukkit.ChatColor;
import org.bukkit.command.CommandSender;

import fr.skytasul.citizenstext.options.OptionPlaybackTime;
import fr.skytasul.citizenstext.options.OptionRepeat;

public class ArgumentTextPlaybackTime extends TextCommandArgument<OptionPlaybackTime> {

public ArgumentTextPlaybackTime() {
super("playback", "playback", OptionPlaybackTime.class);
}

@Override
public boolean createTextInstance() {
return true;
}

@Override
public boolean onCommand(CommandSender sender, String[] args, OptionPlaybackTime option) {
if (!option.getTextInstance().getOption(OptionRepeat.class).getOrDefault()) {
sender.sendMessage("The \"no repeat\" option is enabled. You must first disable it using \"\text repeat\" before editing the playback time.");
return false;
}
Integer oldTime = option.getValue();
if (args.length == 0) {
if (oldTime == null) {
sender.sendMessage("§cNo custom playback time was set. Default value: " + option.getDefault());
return false;
}
option.setValue(null);
sender.sendMessage("§aCustom playback time removed. (old: \"" + oldTime + "§r§a\")");
}else {
try {
int time = Integer.parseInt(args[0]);
if (time < 0) {
sender.sendMessage(ChatColor.RED + args[0] + " is not a valid number. (must be positive)");
return false;
}
option.setValue(time);
sender.sendMessage("§aCustom playback time set to " + time + " seconds. (old: \"" + oldTime + "§r§a\")");
}catch (IllegalArgumentException ex) {
sender.sendMessage(ChatColor.RED + "\"" + args[0] + "\" is not a valid number.");
return false;
}
}
return true;
}

@Override
public String getHelpString() {
return super.getHelpString() + " <time in seconds> : Set the time before players can restart the conversation";
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public TextCommand() {
registerArgument(new ArgumentMessageSetSound());
registerArgument(new ArgumentTextName());
registerArgument(new ArgumentTextRepeat());
registerArgument(new ArgumentTextPlaybackTime());
registerArgument(new ArgumentTextRandom());
registerArgument(new ArgumentTextNear());
registerArgument(new ArgumentMessageList());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public boolean onCommand(CommandSender sender, String[] args, OptionMessages opt
try {
id = Integer.parseInt(args[0]);
}catch (IllegalArgumentException ex) {
sender.sendMessage(ChatColor.RED + "\"" + args[0] + "\" isn't a valid number.");
sender.sendMessage(ChatColor.RED + "\"" + args[0] + "\" is not a valid number.");
return false;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package fr.skytasul.citizenstext.options;

import org.bukkit.configuration.ConfigurationSection;

import fr.skytasul.citizenstext.CitizensTextConfiguration;
import fr.skytasul.citizenstext.texts.TextInstance;

public class OptionPlaybackTime extends TextOption<Integer> {

public OptionPlaybackTime(TextInstance txt) {
super(txt);
}

@Override
public Integer getDefault() {
return CitizensTextConfiguration.getTimeToPlayback();
}

@Override
protected void saveValue(ConfigurationSection config, String key) {
config.set(key, getValue());
}

@Override
protected Integer loadValue(ConfigurationSection config, String key) {
return config.getInt(key);
}

@Override
public void setDefaultValue() {}

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public TextOptionsRegistry() {
register(new TextOptionType<>(OptionRandom.class, OptionRandom::new, "random"));
register(new TextOptionType<>(OptionRepeat.class, OptionRepeat::new, "repeat"));
register(new TextOptionType<>(OptionNear.class, OptionNear::new, "near"));
register(new TextOptionType<>(OptionPlaybackTime.class, OptionPlaybackTime::new, "playbackTime"));
}

public void register(TextOptionType<?> optionType) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import fr.skytasul.citizenstext.options.OptionMessages;
import fr.skytasul.citizenstext.options.OptionName;
import fr.skytasul.citizenstext.options.OptionNear;
import fr.skytasul.citizenstext.options.OptionPlaybackTime;
import fr.skytasul.citizenstext.options.OptionRandom;
import fr.skytasul.citizenstext.options.OptionRepeat;
import fr.skytasul.citizenstext.options.TextOption;
Expand Down Expand Up @@ -219,13 +220,15 @@ public void send(Player p, CTPlayerText playerText) {
id++;
if (messages.messagesSize() == id) { // last message
playerText.removeMessage();
if (!isRepeat()) {
if (isRepeat()) {
int playback = getOption(OptionPlaybackTime.class).getOrDefault();
if (playback > 0) playerText.setTime(System.currentTimeMillis() + playback * 1000);
}else {
playerText.setNoRepeat();
}else if (CitizensTextConfiguration.getTimeToPlayback() > 0) {
playerText.setTime(System.currentTimeMillis() + CitizensTextConfiguration.getTimeToPlayback() * 1000);
}
return;
}

// not last message
if (CitizensTextConfiguration.getKeepTime() != -1) playerText.setResetTime(System.currentTimeMillis() + CitizensTextConfiguration.getKeepTime() * 1000);
playerText.setMessage(id);
Expand Down

0 comments on commit 0f73653

Please sign in to comment.