Skip to content

Commit

Permalink
Rewrote config system, adding blocks.yml
Browse files Browse the repository at this point in the history
#27 - Added Cars placeable material limitation
#31 - Added ability to limit number of active owned cars per player
#30 - Added Car Launching
Added "/carz add (speed/climb/placeable/launch) (material) [amount]" and "/carz remove (...)"
  • Loading branch information
A5H73Y committed May 23, 2020
1 parent 3cae246 commit 77bf7e8
Show file tree
Hide file tree
Showing 22 changed files with 886 additions and 591 deletions.
95 changes: 66 additions & 29 deletions src/main/java/io/github/a5h73y/carz/Carz.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@
import io.github.a5h73y.carz.commands.CarzAutoTabCompleter;
import io.github.a5h73y.carz.commands.CarzCommands;
import io.github.a5h73y.carz.commands.CarzConsoleCommands;
import io.github.a5h73y.carz.configuration.Settings;
import io.github.a5h73y.carz.configuration.CarzConfiguration;
import io.github.a5h73y.carz.configuration.ConfigManager;
import io.github.a5h73y.carz.configuration.impl.DefaultConfig;
import io.github.a5h73y.carz.controllers.CarController;
import io.github.a5h73y.carz.controllers.FuelController;
import io.github.a5h73y.carz.enums.ConfigType;
import io.github.a5h73y.carz.listeners.PlayerListener;
import io.github.a5h73y.carz.listeners.SignListener;
import io.github.a5h73y.carz.listeners.VehicleListener;
Expand All @@ -29,18 +32,26 @@ public class Carz extends JavaPlugin {

private FuelController fuelController;
private CarController carController;
private Settings settings;
private ConfigManager configManager;
private ItemMetaUtils itemMetaUtils;

/**
* Get the plugin's instance.
*
* @return Carz plugin instance.
*/
public static Carz getInstance() {
return instance;
}

/**
* Initialise the Carz plugin.
*/
@Override
public void onEnable() {
instance = this;

settings = new Settings(this);
configManager = new ConfigManager(this.getDataFolder());
carController = new CarController(this);
fuelController = new FuelController(this);
itemMetaUtils = new ItemMetaUtils();
Expand All @@ -52,46 +63,47 @@ public void onEnable() {

getLogger().info("Enabled Carz v" + getDescription().getVersion());
new MetricsLite(this, BUKKIT_PLUGIN_ID);
updatePlugin();
checkForUpdates();
}

/**
* Shutdown the plugin.
*/
@Override
public void onDisable() {
PluginUtils.log("Disabled Carz v" + getDescription().getVersion());
instance = null;
}

private void setupPlugins() {
bountifulAPI = new BountifulAPI();
economyAPI = new EconomyAPI();
}

private void registerCommands() {
getCommand("carz").setExecutor(new CarzCommands(this));
getCommand("carzc").setExecutor(new CarzConsoleCommands(this));
if (getConfig().getBoolean("Other.UseAutoTabCompletion")) {
getCommand("carz").setTabCompleter(new CarzAutoTabCompleter(this));
}
}

private void registerEvents() {
getServer().getPluginManager().registerEvents(new VehicleListener(this), this);
getServer().getPluginManager().registerEvents(new PlayerListener(this), this);
getServer().getPluginManager().registerEvents(new SignListener(this), this);
}

/**
* The Carz message prefix.
* @return carz prefix from the config.
*/
public static String getPrefix() {
return TranslationUtils.getTranslation("Carz.Prefix", false);
}

private void updatePlugin() {
if (getConfig().getBoolean("Other.UpdateCheck")) {
new CarzUpdater(this, SPIGOT_PLUGIN_ID).checkForUpdateAsync();
}
/**
* Get the matching {@link CarzConfiguration} for the given {@link ConfigType}.
*
* @param type {@link ConfigType}
* @return matching {@link CarzConfiguration}
*/
public static CarzConfiguration getConfig(ConfigType type) {
return instance.configManager.get(type);
}

/**
* Get the default config.yml file.
*
* @return {@link DefaultConfig}
*/
public static DefaultConfig getDefaultConfig() {
return (DefaultConfig) instance.configManager.get(ConfigType.DEFAULT);
}

public Settings getSettings() {
return settings;
public ConfigManager getConfigManager() {
return configManager;
}

public FuelController getFuelController() {
Expand All @@ -113,4 +125,29 @@ public EconomyAPI getEconomyAPI() {
public ItemMetaUtils getItemMetaUtils() {
return itemMetaUtils;
}

private void setupPlugins() {
bountifulAPI = new BountifulAPI();
economyAPI = new EconomyAPI();
}

private void registerCommands() {
getCommand("carz").setExecutor(new CarzCommands(this));
getCommand("carzc").setExecutor(new CarzConsoleCommands(this));
if (getConfig().getBoolean("Other.UseAutoTabCompletion")) {
getCommand("carz").setTabCompleter(new CarzAutoTabCompleter(this));
}
}

private void registerEvents() {
getServer().getPluginManager().registerEvents(new VehicleListener(this), this);
getServer().getPluginManager().registerEvents(new PlayerListener(this), this);
getServer().getPluginManager().registerEvents(new SignListener(this), this);
}

private void checkForUpdates() {
if (getConfig().getBoolean("Other.UpdateCheck")) {
new CarzUpdater(this, SPIGOT_PLUGIN_ID).checkForUpdateAsync();
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.github.a5h73y.carz.commands;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import io.github.a5h73y.carz.Carz;
Expand All @@ -18,6 +19,8 @@
*/
public class CarzAutoTabCompleter extends AbstractPluginReceiver implements TabCompleter {

private final List<String> addRemoveList = Arrays.asList("climb", "speed", "launch", "placeable");

public CarzAutoTabCompleter(final Carz carz) {
super(carz);
}
Expand All @@ -32,14 +35,28 @@ public List<String> onTabComplete(CommandSender sender, Command command, String
return null;
}

if (args.length > 1) {
return new ArrayList<>();
}

final Player player = (Player) sender;
List<String> allowedCommands = new ArrayList<>();
List<String> filteredCommands = new ArrayList<>();

if (args.length == 1) {
allowedCommands = populateMainCommands(player);

} else if (args.length == 2) {
allowedCommands = populateChildCommands(player, args[0].toLowerCase());
}

for (String allowedCommand : allowedCommands) {
if (allowedCommand.startsWith(args[args.length - 1])) {
filteredCommands.add(allowedCommand);
}
}

return filteredCommands.isEmpty() ? allowedCommands : filteredCommands;
}

private List<String> populateMainCommands(Player player) {
List<String> allowedCommands = new ArrayList<>();
allowedCommands.add("cmds");
allowedCommands.add("claim");
allowedCommands.add("details");
Expand Down Expand Up @@ -67,10 +84,8 @@ public List<String> onTabComplete(CommandSender sender, Command command, String
}

if (PermissionUtils.hasStrictPermission(player, Permissions.ADMIN, false)) {
allowedCommands.add("addclimb");
allowedCommands.add("addspeed");
allowedCommands.add("removeclimb");
allowedCommands.add("removespeed");
allowedCommands.add("add");
allowedCommands.add("remove");
allowedCommands.add("createtype");
allowedCommands.add("economy");
allowedCommands.add("reload");
Expand All @@ -80,12 +95,19 @@ public List<String> onTabComplete(CommandSender sender, Command command, String
}
}

for (String allowedCommand : allowedCommands) {
if (allowedCommand.startsWith(args[args.length - 1])) {
filteredCommands.add(allowedCommand);
}
return allowedCommands;
}

private List<String> populateChildCommands(Player player, String command) {
List<String> allowedCommands = new ArrayList<>();

switch (command) {
case "add":
case "remove":
allowedCommands = addRemoveList;
break;
}

return filteredCommands.isEmpty() ? allowedCommands : filteredCommands;
return allowedCommands;
}
}
37 changes: 6 additions & 31 deletions src/main/java/io/github/a5h73y/carz/commands/CarzCommands.java
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,6 @@ public boolean onCommand(CommandSender sender, Command command, String label, St
return false;
}

if (!DelayTasks.getInstance().delayPlayer(player, 4)) {
return false;
}

CarUtils.givePlayerCar(player, args.length > 1 ? args[1] : DEFAULT_CAR);
TranslationUtils.sendTranslation("Car.Spawned", player);
break;
Expand Down Expand Up @@ -128,44 +124,23 @@ public boolean onCommand(CommandSender sender, Command command, String label, St
carz.getCarController().stashCar(player);
break;

case "addcb":
case "addclimb":
case "addclimbblock":
case "add":
if (!PermissionUtils.hasStrictPermission(player, Permissions.ADMIN)) {
return false;
}

PluginUtils.addClimbBlock(player, args);
break;

case "removecb":
case "removeclimb":
case "removeclimbblock":
if (!PermissionUtils.hasStrictPermission(player, Permissions.ADMIN)) {
return false;
}

PluginUtils.removeClimbBlock(player, args);
break;

case "addsb":
case "addspeed":
case "addspeedblock":
if (!PermissionUtils.hasStrictPermission(player, Permissions.ADMIN)) {
} else if (!PluginUtils.validateArgs(player, args, 3, 4)) {
return false;
}

PluginUtils.addSpeedBlock(player, args);
PluginUtils.addBlockType(player, args);
break;

case "removesb":
case "removespeed":
case "removespeedblock":
case "remove":
if (!PermissionUtils.hasStrictPermission(player, Permissions.ADMIN)) {
return false;
}

PluginUtils.removeSpeedBlock(player, args);
PluginUtils.removeBlockType(player, args);
break;

case "createtype":
Expand Down Expand Up @@ -240,7 +215,7 @@ public boolean onCommand(CommandSender sender, Command command, String label, St
return false;
}

carz.getSettings().reload();
Carz.getInstance().getConfigManager().reloadConfigs();
TranslationUtils.sendTranslation("Carz.ConfigReloaded", player);
break;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,28 +61,16 @@ public boolean onCommand(CommandSender sender, Command command, String label, St
TranslationUtils.sendTranslation("Car.Spawned", sender, player);
break;

case "addcb":
case "addclimb":
case "addclimbblock":
PluginUtils.addClimbBlock(sender, args);
break;

case "removecb":
case "removeclimb":
case "removeclimbblock":
PluginUtils.removeClimbBlock(sender, args);
break;
case "add":
if (!PluginUtils.validateArgs(sender, args, 3, 4)) {
return false;
}

case "addsb":
case "addspeed":
case "addspeedblock":
PluginUtils.addSpeedBlock(sender, args);
PluginUtils.addBlockType(sender, args);
break;

case "removesb":
case "removespeed":
case "removespeedblock":
PluginUtils.removeSpeedBlock(sender, args);
case "remove":
PluginUtils.removeBlockType(sender, args);
break;

case "destroyall":
Expand All @@ -91,7 +79,7 @@ public boolean onCommand(CommandSender sender, Command command, String label, St
break;

case "reload":
carz.getSettings().reload();
Carz.getInstance().getConfigManager().reloadConfigs();
TranslationUtils.sendTranslation("Carz.ConfigReloaded", sender);
break;

Expand Down
Loading

0 comments on commit 77bf7e8

Please sign in to comment.