Skip to content

Commit

Permalink
add better logging and feedback and better internal reloading
Browse files Browse the repository at this point in the history
  • Loading branch information
kristianvld committed Dec 3, 2020
1 parent 7ff94a7 commit 2ea1125
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,8 @@ public boolean onCommand(CommandSender sender, Command command, String label, St
}
return true;
} else if (args[0].equalsIgnoreCase("reload")) {
if (hasPermission(sender, permissionDeniedMessage)) {
Main.getInstance().reload();
C.main(sender, "Reloaded {0}.", Main.getInstance().getDescription().getName());
if (hasPermission(sender, reloadPermission)) {
Main.getInstance().reload(sender);
}
return true;
} else if (!(sender instanceof Player)) {
Expand Down
23 changes: 17 additions & 6 deletions src/main/java/com/github/kristianvld/angeltrophies/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
import com.github.kristianvld.angeltrophies.skin.SkinManager;
import com.github.kristianvld.angeltrophies.trophy.Trophy;
import com.github.kristianvld.angeltrophies.trophy.TrophyManager;
import com.github.kristianvld.angeltrophies.util.C;
import org.bukkit.Material;
import org.bukkit.command.CommandSender;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.event.HandlerList;
Expand Down Expand Up @@ -49,12 +51,21 @@ public void onEnable() {
}
}

public void reload() {
HandlerList.unregisterAll(skinManager);
HandlerList.unregisterAll(trophyManager);
getLogger().info("Reloading...");
loadManagers();
getLogger().info("Done Reloading.");
public void reload(CommandSender sender) {
try {
HandlerList.unregisterAll(skinManager);
HandlerList.unregisterAll(trophyManager);
getLogger().info("Reloading...");
loadManagers();
getLogger().info("Done Reloading.");
C.main(sender, "Reloaded {0}.", Main.getInstance().getDescription().getName());
} catch (Exception e) {
getLogger().log(Level.SEVERE, "Error while reloading:", e);
getLogger().severe("An error occurred while enabling AngelTrophies, disabling...");
C.error(sender, "An error occurred while reload {0}.", Main.getInstance().getDescription().getName());
C.error(sender, "Disabling the plugin...");
setEnabled(false);
}
}

private void loadManagers() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public class CouchUtil {
private static final Map<Pair<String, CouchRole>, Trophy> trophies = new HashMap<>();

public static void buildCache(Collection<Trophy> trophies) {
CouchUtil.trophies.clear();
Set<String> groups = new HashSet<>();
for (Trophy t : trophies) {
if (t.getCouchRole() != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public Skin(Material sourceType, int sourceID, String target, String sticker) {

public Skin(String source, String target, String sticker) {
if (!OraxenItems.exists(source)) {
throw new IllegalArgumentException("Source is not a valid Oraxen item!");
throw new IllegalArgumentException("Source is not a valid Oraxen item for Skin '" + sticker + "'. '" + source + "' was not found.");
}
ItemStack sourceItem = OraxenItems.getItemById(source).build();
init(sourceItem.getType(), getModelData(sourceItem), target, sticker);
Expand All @@ -46,14 +46,14 @@ private void init(Material sourceType, int sourceID, String target, String stick
material = sourceType;
this.sourceID = sourceID;
if (!OraxenItems.exists(target)) {
throw new IllegalArgumentException("Target is not a valid Oraxen item!");
throw new IllegalArgumentException("Target is not a valid Oraxen item for the Skin '" + sticker + "', target: '" + target + "'!");
}
if (!OraxenItems.exists(sticker)) {
throw new IllegalArgumentException("Sticker is not a valid Oraxen item!");
throw new IllegalArgumentException("Skin is not a valid Oraxen item for the Skin '" + sticker + "'!");
}
ItemStack targetItem = OraxenItems.getItemById(target).build();
if (targetItem.getType() != sourceType) {
throw new IllegalArgumentException("Source and Target needs to be of the same material!");
throw new IllegalArgumentException("Source and Target needs to be of the same material for the Skin '" + sticker + "'. Provided target: " + targetItem.getType() + ", provided source: " + sourceType + "!");
}
targetID = getModelData(targetItem);
targetName = target;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public Trophy(String item, boolean floor, boolean floorSmall, double floorOffset
itemName = item;
exampleItem = OraxenItems.exists(item) ? OraxenItems.getItemById(item).build() : null;
if (exampleItem == null || exampleItem.getAmount() <= 0) {
throw new IllegalArgumentException("Invalid Oraxen item provided for trophy.");
throw new IllegalArgumentException("Invalid Oraxen item provided for trophy '" + item + "'.");
}
this.floor = floor;
this.floorSmall = floorSmall;
Expand All @@ -70,7 +70,7 @@ public Trophy(String item, boolean floor, boolean floorSmall, double floorOffset
this.couchGroup = couchGroup;
this.couchRole = couchRole;
if ((couchGroup == null || couchRole == null) && (couchGroup != null || couchRole != null)) {
throw new IllegalArgumentException("Both CouchGroup and CouchRole needs to be define, not just one.");
throw new IllegalArgumentException("Both CouchGroup and CouchRole needs to be define, not just one for the trophy '" + item + "'.");
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,17 @@ public static String prefix(String prefix, String colorBody, String msg, Object.
}

public static void main(CommandSender receiver, String msg, Object... objs) {
if (receiver == null) {
return;
}
String message = prefix(prefixMain, colorBodyMain, msg, objs);
receiver.sendMessage(message);
}

public static void error(CommandSender receiver, String msg, Object... objs) {
if (receiver == null) {
return;
}
String message = prefix(prefixError, colorBodyError, msg, objs);
receiver.sendMessage(message);
}
Expand Down

0 comments on commit 2ea1125

Please sign in to comment.