Tab complete, valid world reloading, onDisable message

This commit is contained in:
Esophose 2019-02-06 20:37:16 -07:00
parent 5cad56e436
commit 83ceca627c
5 changed files with 84 additions and 48 deletions

View File

@ -1,22 +1,24 @@
package com.songoda.ultimatetimber;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.UUID;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.World;
import org.bukkit.command.CommandSender;
import org.bukkit.command.PluginCommand;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;
import com.songoda.ultimatetimber.commands.CommandHandler;
import com.songoda.ultimatetimber.configurations.DefaultConfig;
import com.songoda.ultimatetimber.treefall.CustomLoot;
import com.songoda.ultimatetimber.treefall.TreeFallAnimation;
import com.songoda.ultimatetimber.treefall.TreeFallListener;
import com.songoda.ultimatetimber.utils.Methods;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.World;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.UUID;
/*
Note: In this plugin, I have called the act of a tree falling over with pseudo-physics "toppling over". This is reflected
@ -25,10 +27,12 @@ PS: MagmaGuy was here
*/
public class UltimateTimber extends JavaPlugin {
private static CommandSender console = Bukkit.getConsoleSender();
private final static CommandSender console = Bukkit.getConsoleSender();
private static UltimateTimber INSTANCE;
private final String prefix = "&8[&6UltimateTimber&8]";
private List<World> validWorlds = new ArrayList<>();
private Set<String> validWorlds;
private List<UUID> isNotChopping = new ArrayList<>();
public static UltimateTimber getInstance() {
@ -66,23 +70,39 @@ public class UltimateTimber extends JavaPlugin {
CustomLoot.initializeCustomItems();
/*
Cache valid worlds for later use
Cache valid world names for later use
*/
for (World world : Bukkit.getWorlds())
if (getConfig().getBoolean(DefaultConfig.VALID_WORLDS + world.getName()))
validWorlds.add(world);
this.reloadValidWorlds();
/*
Register command executor and tab completer
*/
PluginCommand ultimatetimber = this.getCommand("ultimatetimber");
CommandHandler commandHandler = new CommandHandler();
ultimatetimber.setExecutor(commandHandler);
ultimatetimber.setTabCompleter(commandHandler);
this.getCommand("ultimatetimber").setExecutor(new CommandHandler(this));
console.sendMessage(Methods.formatText("&a============================="));
}
@Override
public void onDisable() {
validWorlds.clear();
console.sendMessage(Methods.formatText("&a============================="));
console.sendMessage(Methods.formatText("&7" + this.getDescription().getName() + " " + this.getDescription().getVersion() + " by &5Songoda <3&7!"));
console.sendMessage(Methods.formatText("&7Action: &cDisabling&7..."));
this.validWorlds.clear();
this.isNotChopping.clear();
console.sendMessage(Methods.formatText("&a============================="));
}
public void reloadValidWorlds() {
this.validWorlds = this.getConfig().getConfigurationSection(DefaultConfig.VALID_WORLDS).getKeys(false);
}
public List<World> getValidWorlds() {
return Collections.unmodifiableList(validWorlds);
public boolean isWorldValid(World world) {
return this.validWorlds.contains(world.getName());
}
public String getPrefix() {
@ -96,9 +116,9 @@ public class UltimateTimber extends JavaPlugin {
if (currentVersion < workingVersion) {
Bukkit.getScheduler().scheduleSyncDelayedTask(this, () -> {
Bukkit.getConsoleSender().sendMessage("");
Bukkit.getConsoleSender().sendMessage(String.format("%sYou installed the 1.%s only version of %s on a 1.%s server. Since you are on the wrong version we disabled the plugin for you. Please install correct version to continue using %s.", ChatColor.RED, workingVersion, this.getDescription().getName(), currentVersion, this.getDescription().getName()));
Bukkit.getConsoleSender().sendMessage("");
console.sendMessage("");
console.sendMessage(String.format("%sYou installed the 1.%s only version of %s on a 1.%s server. Since you are on the wrong version we disabled the plugin for you. Please install correct version to continue using %s.", ChatColor.RED, workingVersion, this.getDescription().getName(), currentVersion, this.getDescription().getName()));
console.sendMessage("");
}, 20L);
return false;
}
@ -106,17 +126,14 @@ public class UltimateTimber extends JavaPlugin {
}
public boolean toggleChopping(Player player) {
if (!isNotChopping.contains(player.getUniqueId())) {
isNotChopping.add(player.getUniqueId());
return false;
}
isNotChopping.remove(player.getUniqueId());
return true;
boolean removed = this.isNotChopping.remove(player.getUniqueId());
if (!removed)
this.isNotChopping.add(player.getUniqueId());
return removed;
}
public boolean isChopping(Player player) {
return !isNotChopping.contains(player.getUniqueId());
return !this.isNotChopping.contains(player.getUniqueId());
}
}

View File

@ -1,24 +1,24 @@
package com.songoda.ultimatetimber.commands;
import com.songoda.ultimatetimber.UltimateTimber;
import com.songoda.ultimatetimber.utils.Methods;
import org.bukkit.ChatColor;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.command.TabCompleter;
import org.bukkit.entity.Player;
import org.bukkit.util.StringUtil;
public class CommandHandler implements CommandExecutor {
import com.songoda.ultimatetimber.UltimateTimber;
import com.songoda.ultimatetimber.utils.Methods;
private final UltimateTimber plugin;
public CommandHandler(UltimateTimber plugin) {
this.plugin = plugin;
}
public class CommandHandler implements CommandExecutor, TabCompleter {
@Override
public boolean onCommand(CommandSender commandSender, Command command, String s, String[] args) {
if (args.length > 0)
if (args[0].equalsIgnoreCase("reload")) {
if (commandSender instanceof Player && !permCheck((Player) commandSender, "ultimatetimber.reload")) {
@ -39,7 +39,7 @@ public class CommandHandler implements CommandExecutor {
}
commandSender.sendMessage("");
commandSender.sendMessage(Methods.formatText(plugin.getPrefix() + " &7Version " + plugin.getDescription().getVersion() + " Created with <3 by &5&l&oSongoda"));
commandSender.sendMessage(Methods.formatText(UltimateTimber.getInstance().getPrefix() + " &7Version " + UltimateTimber.getInstance().getDescription().getVersion() + " Created with <3 by &5&l&oSongoda"));
commandSender.sendMessage(Methods.formatText("&8 - &a/ut reload &7 - Reloads the config."));
commandSender.sendMessage(Methods.formatText("&8 - &a/ut toggle &7 - Toggles your chopping mode"));
commandSender.sendMessage("");
@ -55,5 +55,24 @@ public class CommandHandler implements CommandExecutor {
return true;
}
@Override
public List<String> onTabComplete(CommandSender commandSender, Command command, String s, String[] args) {
List<String> completions = new ArrayList<>();
if (!(commandSender instanceof Player) || args.length == 0 || args.length > 1)
return completions;
Set<String> possibleCompletions = new HashSet<>();
if (permCheck((Player) commandSender, "ultimatetimber.reload"))
possibleCompletions.add("reload");
if (permCheck((Player) commandSender, "ultimatetimber.toggle"))
possibleCompletions.add("toggle");
StringUtil.copyPartialMatches(args[0], possibleCompletions, completions);
return completions;
}
}

View File

@ -1,17 +1,17 @@
package com.songoda.ultimatetimber.commands;
import org.bukkit.command.CommandSender;
import com.songoda.ultimatetimber.UltimateTimber;
import com.songoda.ultimatetimber.treefall.CustomLoot;
import com.songoda.ultimatetimber.utils.Methods;
import org.bukkit.ChatColor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
class ReloadCommand {
static void reloadConfig(CommandSender commandSender) {
UltimateTimber plugin = UltimateTimber.getInstance();
plugin.reloadConfig();
plugin.reloadValidWorlds();
CustomLoot.initializeCustomItems();
commandSender.sendMessage(Methods.formatText(plugin.getPrefix() + " &7Configuration reloaded"));
}

View File

@ -20,7 +20,7 @@ class EventFilter {
General catchers
*/
if (event.isCancelled()
|| !plugin.getValidWorlds().contains(event.getPlayer().getWorld())
|| !plugin.isWorldValid(event.getPlayer().getWorld())
|| !TreeChecker.validMaterials.contains(event.getBlock().getType())) return false;
FileConfiguration fileConfiguration = UltimateTimber.getInstance().getConfig();

View File

@ -30,7 +30,7 @@ public class LeafToSaplingConverter {
material = org.bukkit.Material.SPRUCE_SAPLING;
break;
default:
material = material;
break;
}