Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import java.util.Set;
import java.util.logging.Level;

import com.bergerkiller.bukkit.lightcleaner.messages.MessageHandler;
import com.bergerkiller.bukkit.lightcleaner.messages.TranslatableMessage;
import org.bukkit.ChatColor;
import org.bukkit.World;
import org.bukkit.command.CommandSender;
Expand All @@ -24,6 +26,7 @@
import com.bergerkiller.bukkit.lightcleaner.util.DelayClosedForcedChunk;

public class LightCleaner extends PluginBase {
private static MessageHandler messageHandler;
public static LightCleaner plugin;
public static long minFreeMemory = 100 * 1024 * 1024;
public static boolean autoCleanEnabled = false;
Expand Down Expand Up @@ -55,6 +58,10 @@ public void permissions() {
this.loadPermissions(Permission.class);
}

public static MessageHandler getMessageHandler() {
return messageHandler;
}

@Override
public void enable() {
plugin = this;
Expand Down Expand Up @@ -106,6 +113,9 @@ public void enable() {

config.save();

//Load messages
messageHandler = new MessageHandler();

// Warn if no memory limit is set
if (Runtime.getRuntime().maxMemory() == Long.MAX_VALUE && minFreeMemory > 0) {
log(Level.WARNING, "No memory limitation is configured for Java. An out of memory condition might occur for large operations!");
Expand Down Expand Up @@ -173,17 +183,24 @@ public boolean command(CommandSender sender, String command, String[] args) {
Permission.BLOCK_DEBUG.handle(sender);
if (args.length >= 2 && args[1].equalsIgnoreCase("clear")) {
LightingCube.DEBUG_BLOCK = null;
sender.sendMessage(ChatColor.GREEN + "Cleared block being debugged");
sender.sendMessage(TranslatableMessage.of("cleanlight.clear.block.debugged"));
//sender.sendMessage(ChatColor.GREEN + "Cleared block being debugged");
} else if (args.length <= 3) {
sender.sendMessage(ChatColor.RED + "/cleanlight debugblock <x> <y> <z>");
sender.sendMessage(ChatColor.RED + "/cleanlight debugblock clear");
sender.sendMessage(TranslatableMessage.of("cleanlight.usage.debugblock"));
//sender.sendMessage(ChatColor.RED + "/cleanlight debugblock <x> <y> <z>");
sender.sendMessage(TranslatableMessage.of("cleanlight.usage.debugblock.clear"));
//sender.sendMessage(ChatColor.RED + "/cleanlight debugblock clear");
} else {
int x = ParseUtil.parseInt(args[1], 0);
int y = ParseUtil.parseInt(args[2], 0);
int z = ParseUtil.parseInt(args[3], 0);
LightingCube.DEBUG_BLOCK = new IntVector3(x, y, z);
sender.sendMessage(ChatColor.GREEN + "Will show generated levels for block " +
x + "/" + y + "/" + z);
sender.sendMessage(String.format(TranslatableMessage.of("show.generated.levels"),
x, y, z
)
);
//sender.sendMessage(ChatColor.GREEN + "Will show generated levels for block " +
// x + "/" + y + "/" + z);
}
return true;
}
Expand All @@ -192,27 +209,36 @@ public boolean command(CommandSender sender, String command, String[] args) {
Permission.ABORT.handle(sender);
if (LightingService.isProcessing()) {
LightingService.clearTasks();
sender.sendMessage(ChatColor.GREEN + "All pending tasks cleared.");
sender.sendMessage(TranslatableMessage.of("cleanlight.pending.tasks.cleared"));
//sender.sendMessage(ChatColor.GREEN + "All pending tasks cleared.");
} else {
sender.sendMessage(ChatColor.YELLOW + "No lighting was being processed; there was nothing to abort.");
sender.sendMessage(TranslatableMessage.of("cleanlight.no.lighting.aborted"));
//sender.sendMessage(ChatColor.YELLOW + "No lighting was being processed; there was nothing to abort.");
}
return true;
}
if (subCmd.equalsIgnoreCase("pause")) {
// cleanlight pause
Permission.PAUSE.handle(sender);
LightingService.setPaused(true);
sender.sendMessage(ChatColor.YELLOW + "Light cleaning " + ChatColor.RED + "paused");
sender.sendMessage(TranslatableMessage.of("cleanlight.lighting.paused"));
//sender.sendMessage(ChatColor.YELLOW + "Light cleaning " + ChatColor.RED + "paused");
if (LightingService.isProcessing()) {
sender.sendMessage(ChatColor.RED.toString() + LightingService.getChunkFaults() + ChatColor.YELLOW + " chunks are pending");

sender.sendMessage(String.format(TranslatableMessage.of("cleanlight.pending.chunks"),
LightingService.getChunkFaults()
)
);
//sender.sendMessage(ChatColor.RED.toString() + LightingService.getChunkFaults() + ChatColor.YELLOW + " chunks are pending");
}
return true;
}
if (subCmd.equalsIgnoreCase("resume")) {
// cleanlight resume
Permission.PAUSE.handle(sender);
LightingService.setPaused(false);
sender.sendMessage(ChatColor.YELLOW + "Light cleaning " + ChatColor.GREEN + "resumed");
sender.sendMessage(TranslatableMessage.of("cleanlight.lighting.resumed"));
//sender.sendMessage(ChatColor.YELLOW + "Light cleaning " + ChatColor.GREEN + "resumed");
if (LightingService.isProcessing()) {
sender.sendMessage(ChatColor.RED.toString() + LightingService.getChunkFaults() + ChatColor.YELLOW + " chunks will now be processed");
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package com.bergerkiller.bukkit.lightcleaner.messages;

import com.bergerkiller.bukkit.lightcleaner.LightCleaner;
import com.google.common.base.Preconditions;
import com.google.common.collect.Maps;
import com.google.common.io.Files;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.stream.JsonReader;
import org.bukkit.Bukkit;

import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.Map;
import java.util.logging.Level;

public class MessageHandler {
private static final Gson GSON = new GsonBuilder().setPrettyPrinting().disableHtmlEscaping().create();
private static MessageHandler instance;
private final Map<String, String> messages = Maps.newHashMap();

public MessageHandler() {
instance = this;
LightCleaner plugin = LightCleaner.plugin;
plugin.saveResource("messages.json", false);
try (final JsonReader reader = GSON.newJsonReader(Files
.newReader(new File(plugin.getDataFolder(), "messages.json"),
StandardCharsets.UTF_8))) {
final JsonObject object = GSON.fromJson(reader, JsonObject.class);
for (final Map.Entry<String, JsonElement> elements : object.entrySet()) {
messages.put(elements.getKey(), elements.getValue().getAsString());
}
} catch (final IOException e) {
Bukkit.getLogger().log(Level.WARNING, "Failed to load messages "+ e);
}
}

/**
* Get a translation from a translation key
*
* @param key Translation Key
* @return Translation
* @throws IllegalArgumentException If the translation does not exist
*/
public String getTranslation(final String key) {
Preconditions.checkNotNull(key, "Key cannot be null");
final String value = this.messages.get(key);
if (value == null) {
throw new IllegalArgumentException("There is no message with that key: " + key);
}
return value;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.bergerkiller.bukkit.lightcleaner.messages;

import com.bergerkiller.bukkit.lightcleaner.LightCleaner;
import org.bukkit.ChatColor;

public class TranslatableMessage {
private final String key;

private TranslatableMessage(final String key) {
this.key = key;
}

/**
* Create a new translatable message with a given message key
*
* @param key Message key
* @return Message instance
*/
public static String of(final String key) {
return ChatColor.translateAlternateColorCodes('&', LightCleaner.getMessageHandler().getTranslation("prefix") +
LightCleaner.getMessageHandler().getTranslation(key));
}

}