Skip to content

Commit

Permalink
Add var system & spychest
Browse files Browse the repository at this point in the history
  • Loading branch information
JesFot committed Aug 1, 2018
1 parent 450d5dc commit 13f06d6
Show file tree
Hide file tree
Showing 10 changed files with 823 additions and 48 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package me.jesfot.gamingblockplug.command;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;

import me.jesfot.gamingblockplug.permission.StaticPerms;
import me.jesfot.gamingblockplug.plugin.GamingBlockPlug;

public class SpyChestCommand extends CommandBase
{
private final GamingBlockPlug plugin;

public SpyChestCommand(GamingBlockPlug plugin)
{
super("spychest");

this.plugin = plugin;

super.setMinimalPermission(StaticPerms.CMD_SPYCHEST);
super.setRawUsageMessage("/<command> <player> [normal|ender]");
}

@Override
protected boolean executeCommand(CommandSender sender, Command command, String label, String[] args)
{
if (!(sender instanceof Player))
{
sender.sendMessage("You must be a player");
return true;
}
if (args.length < 1 || args.length > 2)
{
return false;
}
Player target = this.plugin.getPlayerExact(args[0]);
if (target == null)
{
sender.sendMessage("Target is null or offline.");
return true;
}
String mode = (args.length == 1) ? "normal" : args[1];
Player player = (Player) sender;
if (mode.equalsIgnoreCase("normal"))
{
player.openInventory(target.getInventory());
}
else if (mode.equalsIgnoreCase("ender"))
{
player.openInventory(target.getEnderChest());
}
else if (mode.equalsIgnoreCase("view"))
{
player.openInventory(target.getOpenInventory());
}
else
{
sender.sendMessage("Bad mode (normal or ender)");
return false;
}
return true;
}

@Override
protected List<String> executeTabComplete(CommandSender sender, Command command, String label, String[] args)
{
if (args.length == 1)
{
return null;
}
else if (args.length == 2)
{
return CommandBase.sortStart(args[1], Arrays.asList("normal", "ender"));
}
return Collections.emptyList();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
package me.jesfot.gamingblockplug.command;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

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

import me.jesfot.gamingblockplug.permission.PermissionHelper;
import me.jesfot.gamingblockplug.permission.StaticPerms;
import me.jesfot.gamingblockplug.plugin.GamingBlockPlug;
import me.jesfot.gamingblockplug.security.VariablesSystem;
import me.jesfot.gamingblockplug.utils.NumberUtils;

public class VarCommand extends CommandBase
{
private final GamingBlockPlug plugin;

public VarCommand(GamingBlockPlug plugin)
{
super("var");

this.plugin = plugin;

super.setMinimalPermission(StaticPerms.CMD_VAR);
super.setRawUsageMessage("/<command> <VariableName> | /<command> unset <VariableName> | \"\n" +
" + \"/<command> set <VariableName> <type> <Value...> | /<command> help <all|RubricName>");
}

@Override
protected boolean executeCommand(CommandSender sender, Command command, String label, String[] args)
{
VariablesSystem varSystem = this.plugin.getSystemManager().getVariablesSystem();
varSystem.loadFromFile();
if(args.length >= 4)
{
if(args[0].equalsIgnoreCase("set"))
{
String name = args[1];
boolean exists = (varSystem.getType(name) != null);
if(!PermissionHelper.testPermission(sender, exists ? StaticPerms.CMD_VAR_RESET : StaticPerms.CMD_VAR_SET, false, null))
{
return true;
}
String type = args[2].toLowerCase();
String value = compile(args, 3, " ");
switch(VariablesSystem.getIdForTypes(type))
{
case 0:
varSystem.storeString(name, value);
break;
case 1:
varSystem.storeInt(name, NumberUtils.toInteger(value, 0));
break;
case 2:
varSystem.storeBool(name, Boolean.getBoolean(value.toLowerCase()));
break;
case 3:
varSystem.storeFloat(name, NumberUtils.toFloat(value, 0));
break;
case 4:
varSystem.storeDouble(name, NumberUtils.toDouble(value, 0));
break;
}
sender.sendMessage("Registered.");
varSystem.storeToFile();
}
else
{
this.sendUsage(sender, label);
}
}
else if(args.length == 2)
{
if(args[0].equalsIgnoreCase("unset"))
{
if(!PermissionHelper.testPermission(sender, StaticPerms.CMD_VAR_UNSET, false, null))
{
return true;
}
String name = args[1];
varSystem.remove(name);
varSystem.storeToFile();
}
else if(args[0].equalsIgnoreCase("help"))
{
if(args[1].equalsIgnoreCase("all"))
{
sender.sendMessage("try /<com> help types".replaceAll("<com>", label));
}
else if(args[1].equalsIgnoreCase("types"))
{
sender.sendMessage("Available types of variable are :");
sender.sendMessage(" - String - 'string' | 'str'");
sender.sendMessage(" - Integer - 'integer' | 'int'");
sender.sendMessage(" - Boolean - 'boolean' | 'bool'");
sender.sendMessage(" - Float - 'float'");
sender.sendMessage(" - Double - 'double'");
return true;
}
else
{
sender.sendMessage("This topic does not exists try /<com> help all".replaceAll("<com>", label));
}
}
else
{
this.sendUsage(sender, label);
}
}
else if(args.length == 1)
{
if(!PermissionHelper.testPermission(sender, StaticPerms.CMD_VAR_PRINT, true, ChatColor.RED
+ "You are not allowed to use the /var <<name>> command. ".replace("<name>", args[0])
+ "Sorry, please contact an administrator if you believe that is an error."))
{
return true;
}
if(varSystem.getToString(args[0]) == null)
{
sender.sendMessage("'" + args[0] + "' does not exists as key value.");
return true;
}
sender.sendMessage("Value: " + ChatColor.translateAlternateColorCodes('&', varSystem.getToString(args[0])));
}
else
{
this.sendUsage(sender, label);
}
return true;
}

@Override
protected List<String> executeTabComplete(CommandSender sender, Command command, String label, String[] args)
{
if (args.length == 1)
{
return CommandBase.sortStart(args[0], Arrays.asList("set", "unset", "help"));
}
if (args.length == 2 && args[0].equalsIgnoreCase("help"))
{
return CommandBase.sortStart(args[0], Arrays.asList("types", "all"));
}
if (args.length == 3 && args[0].equalsIgnoreCase("set"))
{
return CommandBase.sortStart(args[0], Arrays.asList("string", "boolean", "integer", "double", "float"));
}
return Collections.emptyList();
}

public static String compile(String[] list, int firstIndex, String separator)
{
if (firstIndex < 0 || firstIndex >= list.length)
{
return "";
}
String result = list[firstIndex];
for (int i = firstIndex + 1; i < list.length; i++)
{
result += " " + list[i];
}
return result;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import me.jesfot.gamingblockplug.permission.StaticPerms;
import me.jesfot.gamingblockplug.plugin.GamingBlockPlug;
import me.jesfot.gamingblockplug.roles.Role;
import me.jesfot.gamingblockplug.roles.RoleManager;
import me.jesfot.gamingblockplug.security.HalfInBedSystem;
import me.jesfot.gamingblockplug.utils.DataUtils;
import me.unei.configuration.api.IFlatConfiguration;
Expand Down Expand Up @@ -62,6 +63,7 @@ public void onPlayerLogin(final PlayerLoginEvent event)
public void onPlayerJoin(final PlayerJoinEvent event)
{
final GBPPlayer player = this.plugin.getPlayerManager().getPlayer(event.getPlayer());
player.safeReload();

if (this.plugin.isOnlineMode())
{
Expand All @@ -72,6 +74,10 @@ public void onPlayerJoin(final PlayerJoinEvent event)
this.plugin.getLogger().info("Starting Login system for " + player.getHandler().getName() + "...");
this.plugin.getSystemManager().getLoginSystem().autoLogin(player);
}
if (player.getRoleId() == null)
{
player.setRoleId(RoleManager.DEFAULT_NAME);
}
if (PermissionHelper.testPermissionSilent(event.getPlayer(), StaticPerms.LOGIN_MOTD, false))
{
IYAMLConfiguration cfg = this.plugin.getConfigurations().getMainConfig();
Expand All @@ -86,6 +92,7 @@ public void onPlayerJoin(final PlayerJoinEvent event)
}
}
}
player.save();
}

@EventHandler
Expand Down Expand Up @@ -183,6 +190,34 @@ public void onPlayerChat(final AsyncPlayerChatEvent event)
Role role = this.plugin.getRoleManager().get(player.getRoleId());

event.setFormat(role.prependFormat(event.getFormat()));

// Variables.

if (PermissionHelper.testPermissionSilent(event.getPlayer(), StaticPerms.VARS_CHAT, false))
{
StringBuilder msg = new StringBuilder();
if(event.getMessage().contains("${") && event.getMessage().contains("}"))
{
this.plugin.getSystemManager().getVariablesSystem().loadFromFile();
String[] splited = event.getMessage().split("\\$");
for(String arg : splited)
{
if(arg.startsWith("{") && arg.contains("}"))
{
String var = arg.substring(1, arg.indexOf("}"));
arg = this.plugin.getSystemManager().getVariablesSystem().getToString(var)
+ ((arg.endsWith("}") && arg.indexOf("}") == arg.length() - 1)
? "" : arg.substring(arg.indexOf("}") + 1));
}
msg.append(arg);
}
event.setMessage(ChatColor.translateAlternateColorCodes('&', msg.toString()));
}
else
{
event.setMessage(ChatColor.translateAlternateColorCodes('&', event.getMessage()));
}
}
}

//Login
Expand Down
Loading

0 comments on commit 13f06d6

Please sign in to comment.