下面列出了io.netty.util.collection.CharObjectHashMap#cn.nukkit.utils.MainLogger 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public void load() {
this.list = new LinkedHashMap<>();
File file = new File(this.file);
try {
if (!file.exists()) {
file.createNewFile();
this.save();
} else {
LinkedList<TreeMap<String, String>> list = new Gson().fromJson(Utils.readFile(this.file), new TypeToken<LinkedList<TreeMap<String, String>>>() {
}.getType());
for (TreeMap<String, String> map : list) {
BanEntry entry = BanEntry.fromMap(map);
this.list.put(entry.getName(), entry);
}
}
} catch (IOException e) {
MainLogger.getLogger().error("Could not load ban list: ", e);
}
}
public void save() {
this.removeExpired();
try {
File file = new File(this.file);
if (!file.exists()) {
file.createNewFile();
}
LinkedList<LinkedHashMap<String, String>> list = new LinkedList<>();
for (BanEntry entry : this.list.values()) {
list.add(entry.getMap());
}
Utils.writeFile(this.file, new ByteArrayInputStream(new GsonBuilder().setPrettyPrinting().create().toJson(list).getBytes(StandardCharsets.UTF_8)));
} catch (IOException e) {
MainLogger.getLogger().error("Could not save ban list ", e);
}
}
public void enablePlugin(Plugin plugin) {
if (!plugin.isEnabled()) {
try {
for (Permission permission : plugin.getDescription().getPermissions()) {
this.addPermission(permission);
}
plugin.getPluginLoader().enablePlugin(plugin);
} catch (Throwable e) {
MainLogger logger = this.server.getLogger();
if (logger != null) {
logger.logException(new RuntimeException(e));
}
this.disablePlugin(plugin);
}
}
}
public void disablePlugin(Plugin plugin) {
if (plugin.isEnabled()) {
try {
plugin.getPluginLoader().disablePlugin(plugin);
} catch (Exception e) {
MainLogger logger = this.server.getLogger();
if (logger != null) {
logger.logException(e);
}
}
this.server.getScheduler().cancelTask(plugin);
HandlerList.unregisterAll(plugin);
for (Permission permission : plugin.getDescription().getPermissions()) {
this.removePermission(permission);
}
}
}
public void removeFromAll(Item item) {
int count = item.getCount();
for (int i = 0; i < this.size; i++) {
Item target = this.getItem(i);
if (target.equals(item, true, false)) {
count--;
target.count--;
this.setItem(i, target);
if (count <= 0) break;
}
}
if (count != 0) {
MainLogger.getLogger().debug("Unexpected ingredient count (" + count + ") in crafting grid");
}
}
public void load() {
this.list = new LinkedHashMap<>();
File file = new File(this.file);
try {
if (!file.exists()) {
file.createNewFile();
this.save();
} else {
LinkedList<TreeMap<String, String>> list = new Gson().fromJson(Utils.readFile(this.file), new TypeToken<LinkedList<TreeMap<String, String>>>() {
}.getType());
for (TreeMap<String, String> map : list) {
BanEntry entry = BanEntry.fromMap(map);
this.list.put(entry.getName(), entry);
}
}
} catch (IOException e) {
MainLogger.getLogger().error("Could not load ban list: ", e);
}
}
public void save() {
this.removeExpired();
try {
File file = new File(this.file);
if (!file.exists()) {
file.createNewFile();
}
LinkedList<LinkedHashMap<String, String>> list = new LinkedList<>();
for (BanEntry entry : this.list.values()) {
list.add(entry.getMap());
}
Utils.writeFile(this.file, new ByteArrayInputStream(new GsonBuilder().setPrettyPrinting().create().toJson(list).getBytes(StandardCharsets.UTF_8)));
} catch (IOException e) {
MainLogger.getLogger().error("Could not save ban list ", e);
}
}
public void enablePlugin(Plugin plugin) {
if (!plugin.isEnabled()) {
try {
for (Permission permission : plugin.getDescription().getPermissions()) {
this.addPermission(permission);
}
plugin.getPluginLoader().enablePlugin(plugin);
} catch (Throwable e) {
MainLogger logger = this.server.getLogger();
if (logger != null) {
logger.logException(new RuntimeException(e));
}
this.disablePlugin(plugin);
}
}
}
public void disablePlugin(Plugin plugin) {
if (plugin.isEnabled()) {
try {
plugin.getPluginLoader().disablePlugin(plugin);
} catch (Exception e) {
MainLogger logger = this.server.getLogger();
if (logger != null) {
logger.logException(e);
}
}
this.server.getScheduler().cancelTask(plugin);
HandlerList.unregisterAll(plugin);
for (Permission permission : plugin.getDescription().getPermissions()) {
this.removePermission(permission);
}
}
}
@SuppressWarnings("unchecked")
private static void initCreativeItems() {
clearCreativeItems();
Config config = new Config(Config.YAML);
config.load(Server.class.getClassLoader().getResourceAsStream("creativeitems.json"));
List<Map> list = config.getMapList("items");
for (Map map : list) {
try {
addCreativeItem(fromJson(map));
} catch (Exception e) {
MainLogger.getLogger().logException(e);
}
}
}
public void setImage(BufferedImage image) {
try {
if (image.getHeight() != 128 || image.getWidth() != 128) { //resize
this.image = new BufferedImage(128, 128, image.getType());
Graphics2D g = this.image.createGraphics();
g.drawImage(image, 0, 0, 128, 128, null);
g.dispose();
} else {
this.image = image;
}
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(this.image, "png", baos);
this.getNamedTag().putByteArray("Colors", baos.toByteArray());
} catch (IOException e) {
MainLogger.getLogger().logException(e);
}
}
public CraftingManager() {
InputStream recipesStream = Server.class.getClassLoader().getResourceAsStream("recipes.json");
if (recipesStream == null) {
throw new AssertionError("Unable to find recipes.json");
}
Config recipesConfig = new Config(Config.JSON);
recipesConfig.load(recipesStream);
this.loadRecipes(recipesConfig);
String path = Server.getInstance().getDataPath() + "custom_recipes.json";
File filePath = new File(path);
if (filePath.exists()) {
Config customRecipes = new Config(filePath, Config.JSON);
this.loadRecipes(customRecipes);
}
this.rebuildPacket();
MainLogger.getLogger().info("Loaded " + this.recipes.size() + " recipes.");
}
public void load() {
this.list = new LinkedHashMap<>();
File file = new File(this.file);
try {
if (!file.exists()) {
file.createNewFile();
this.save();
} else {
LinkedList<TreeMap<String, String>> list = new Gson().fromJson(Utils.readFile(this.file), new TypeToken<LinkedList<TreeMap<String, String>>>() {
}.getType());
for (TreeMap<String, String> map : list) {
BanEntry entry = BanEntry.fromMap(map);
this.list.put(entry.getName(), entry);
}
}
} catch (IOException e) {
MainLogger.getLogger().error("Could not load ban list: ", e);
}
}
public void save() {
this.removeExpired();
try {
File file = new File(this.file);
if (!file.exists()) {
file.createNewFile();
}
LinkedList<LinkedHashMap<String, String>> list = new LinkedList<>();
for (BanEntry entry : this.list.values()) {
list.add(entry.getMap());
}
Utils.writeFile(this.file, new ByteArrayInputStream(new GsonBuilder().setPrettyPrinting().create().toJson(list).getBytes(StandardCharsets.UTF_8)));
} catch (IOException e) {
MainLogger.getLogger().error("Could not save ban list ", e);
}
}
public void enablePlugin(Plugin plugin) {
if (!plugin.isEnabled()) {
try {
for (Permission permission : plugin.getDescription().getPermissions()) {
this.addPermission(permission);
}
plugin.getPluginLoader().enablePlugin(plugin);
} catch (Throwable e) {
MainLogger logger = this.server.getLogger();
if (logger != null) {
logger.logException(new RuntimeException(e));
}
this.disablePlugin(plugin);
}
}
}
public void disablePlugin(Plugin plugin) {
if (plugin.isEnabled()) {
try {
plugin.getPluginLoader().disablePlugin(plugin);
} catch (Exception e) {
MainLogger logger = this.server.getLogger();
if (logger != null) {
logger.logException(e);
}
}
this.server.getScheduler().cancelTask(plugin);
HandlerList.unregisterAll(plugin);
for (Permission permission : plugin.getDescription().getPermissions()) {
this.removePermission(permission);
}
}
}
public void removeFromAll(Item item) {
int count = item.getCount();
for (int i = 0; i < this.size; i++) {
Item target = this.getItem(i);
if (target.equals(item, true, false)) {
count--;
target.count--;
this.setItem(i, target);
if (count <= 0) break;
}
}
if (count != 0) {
MainLogger.getLogger().debug("Unexpected ingredient count (" + count + ") in crafting grid");
}
}
@Override
public void handleEncapsulated(String identifier, EncapsulatedPacket packet, int flags) {
if (this.players.containsKey(identifier)) {
DataPacket pk = null;
try {
if (packet.buffer.length > 0) {
if (packet.buffer[0] == PING_DataPacket.ID) {
PING_DataPacket pingPacket = new PING_DataPacket();
pingPacket.buffer = packet.buffer;
pingPacket.decode();
this.networkLatency.put(identifier, (int) pingPacket.pingID);
return;
}
pk = this.getPacket(packet.buffer);
if (pk != null) {
pk.decode();
this.players.get(identifier).handleDataPacket(pk);
}
}
} catch (Exception e) {
this.server.getLogger().logException(e);
if (Nukkit.DEBUG > 1 && pk != null) {
MainLogger logger = this.server.getLogger();
// if (logger != null) {
logger.debug("Packet " + pk.getClass().getName() + " 0x" + Binary.bytesToHexString(packet.buffer));
//logger.logException(e);
// }
}
if (this.players.containsKey(identifier)) {
this.handler.blockAddress(this.players.get(identifier).getAddress(), 5);
}
}
}
}
public BufferedImage loadImageFromNBT() {
try {
byte[] data = getNamedTag().getByteArray("Colors");
return ImageIO.read(new ByteArrayInputStream(data));
} catch (IOException e) {
MainLogger.getLogger().logException(e);
}
return null;
}
@Override
public void sendMessage(String message) {
message = this.getServer().getLanguage().translateString(message);
for (String line : message.trim().split("\n")) {
MainLogger.getLogger().info(line);
}
}
@Override
public void sendImportantMessage(String message) {
message = this.getServer().getLanguage().translateString(message);
for (String line : message.trim().split("\n")) {
MainLogger.getLogger().info(line);
}
}
@Override
public boolean execute(CommandSender sender, String commandLabel, String[] args) {
boolean result = false;
ArrayList<String> commands = new ArrayList<>();
for (String formatString : formatStrings) {
try {
commands.add(buildCommand(formatString, args));
} catch (Exception e) {
if (e instanceof IllegalArgumentException) {
sender.sendMessage(TextFormat.RED + e.getMessage());
} else {
sender.sendMessage(new TranslationContainer(TextFormat.RED + "%commands.generic.exception"));
MainLogger logger = sender.getServer().getLogger();
if (logger != null) {
logger.logException(e);
}
}
return false;
}
}
for (String command : commands) {
result |= Server.getInstance().dispatchCommand(sender, command);
}
return result;
}
@Override
public boolean dispatch(CommandSender sender, String cmdLine) {
ArrayList<String> parsed = parseArguments(cmdLine);
if (parsed.size() == 0) {
return false;
}
String sentCommandLabel = parsed.remove(0).toLowerCase();
String[] args = parsed.toArray(new String[parsed.size()]);
Command target = this.getCommand(sentCommandLabel);
if (target == null) {
return false;
}
target.timing.startTiming();
try {
target.execute(sender, sentCommandLabel, args);
} catch (Exception e) {
sender.sendMessage(new TranslationContainer(TextFormat.RED + "%commands.generic.exception"));
this.server.getLogger().critical(this.server.getLanguage().translateString("nukkit.command.exception", cmdLine, target.toString(), Utils.getExceptionMessage(e)));
MainLogger logger = sender.getServer().getLogger();
if (logger != null) {
logger.logException(e);
}
}
target.timing.stopTiming();
return true;
}
public static Entity createEntity(String name, FullChunk chunk, CompoundTag nbt, Object... args) {
Entity entity = null;
if (knownEntities.containsKey(name)) {
Class<? extends Entity> clazz = knownEntities.get(name);
if (clazz == null) {
return null;
}
for (Constructor constructor : clazz.getConstructors()) {
if (entity != null) {
break;
}
if (constructor.getParameterCount() != (args == null ? 2 : args.length + 2)) {
continue;
}
try {
if (args == null || args.length == 0) {
entity = (Entity) constructor.newInstance(chunk, nbt);
} else {
Object[] objects = new Object[args.length + 2];
objects[0] = chunk;
objects[1] = nbt;
System.arraycopy(args, 0, objects, 2, args.length);
entity = (Entity) constructor.newInstance(objects);
}
} catch (Exception e) {
MainLogger.getLogger().logException(e);
}
}
}
return entity;
}
@Override
public void handleEncapsulated(String identifier, EncapsulatedPacket packet, int flags) {
if (this.players.containsKey(identifier)) {
DataPacket pk = null;
try {
if (packet.buffer.length > 0) {
if (packet.buffer[0] == PING_DataPacket.ID) {
PING_DataPacket pingPacket = new PING_DataPacket();
pingPacket.buffer = packet.buffer;
pingPacket.decode();
this.networkLatency.put(identifier, (int) pingPacket.pingID);
return;
}
pk = this.getPacket(packet.buffer);
if (pk != null) {
pk.decode();
this.players.get(identifier).handleDataPacket(pk);
}
}
} catch (Exception e) {
this.server.getLogger().logException(e);
if (Nukkit.DEBUG > 1 && pk != null) {
MainLogger logger = this.server.getLogger();
// if (logger != null) {
logger.debug("Packet " + pk.getClass().getName() + " 0x" + Binary.bytesToHexString(packet.buffer));
//logger.logException(e);
// }
}
if (this.players.containsKey(identifier)) {
this.handler.blockAddress(this.players.get(identifier).getAddress(), 5);
}
}
}
}
protected BufferedImage loadImageFromNBT() {
try {
byte[] data = getNamedTag().getByteArray("Colors");
image = ImageIO.read(new ByteArrayInputStream(data));
return image;
} catch (IOException e) {
MainLogger.getLogger().logException(e);
}
return null;
}
@Override
public void sendMessage(String message) {
message = this.getServer().getLanguage().translateString(message);
for (String line : message.trim().split("\n")) {
MainLogger.getLogger().info(line);
}
}
@Override
public boolean execute(CommandSender sender, String commandLabel, String[] args) {
boolean result = false;
ArrayList<String> commands = new ArrayList<>();
for (String formatString : formatStrings) {
try {
commands.add(buildCommand(formatString, args));
} catch (Exception e) {
if (e instanceof IllegalArgumentException) {
sender.sendMessage(TextFormat.RED + e.getMessage());
} else {
sender.sendMessage(new TranslationContainer(TextFormat.RED + "%commands.generic.exception"));
MainLogger logger = sender.getServer().getLogger();
if (logger != null) {
logger.logException(e);
}
}
return false;
}
}
for (String command : commands) {
result |= Server.getInstance().dispatchCommand(sender, command);
}
return result;
}
@Override
public boolean dispatch(CommandSender sender, String cmdLine) {
ArrayList<String> parsed = parseArguments(cmdLine);
if (parsed.size() == 0) {
return false;
}
String sentCommandLabel = parsed.remove(0).toLowerCase();
String[] args = parsed.toArray(new String[0]);
Command target = this.getCommand(sentCommandLabel);
if (target == null) {
return false;
}
target.timing.startTiming();
try {
target.execute(sender, sentCommandLabel, args);
} catch (Exception e) {
sender.sendMessage(new TranslationContainer(TextFormat.RED + "%commands.generic.exception"));
this.server.getLogger().critical(this.server.getLanguage().translateString("nukkit.command.exception", cmdLine, target.toString(), Utils.getExceptionMessage(e)));
MainLogger logger = sender.getServer().getLogger();
if (logger != null) {
logger.logException(e);
}
}
target.timing.stopTiming();
return true;
}
private static EntityMetadata replaceMetadata(EntityMetadata data, long from, long to) {
boolean changed = false;
for (Integer key : replaceMetadata) {
try {
EntityData ed = data.get(key);
if (ed == null) {
continue;
}
if (ed.getType() != Entity.DATA_TYPE_LONG) {
MainLogger.getLogger().info("Wrong entity data type (" + key + ") expected 'Long' got '" + dataTypeToString(ed.getType()) + "'");
continue;
}
long value = ((LongEntityData) ed).getData();
if (value == from) {
if (!changed) {
data = cloneMetadata(data);
changed = true;
}
data.putLong(key, to);
}
} catch (Exception e) {
MainLogger.getLogger().error("Exception while replacing metadata '" + key + "'", e);
}
}
if (!changed) return null;
return data;
}