2014-04-29 10:54:20 +10:00
|
|
|
package codechicken.nei;
|
|
|
|
|
|
2022-06-24 12:32:13 +01:00
|
|
|
import java.io.File;
|
|
|
|
|
import java.io.FileReader;
|
|
|
|
|
import java.io.PrintWriter;
|
|
|
|
|
import java.util.Arrays;
|
|
|
|
|
import java.util.Collection;
|
|
|
|
|
import java.util.Collections;
|
|
|
|
|
import java.util.HashMap;
|
|
|
|
|
import java.util.HashSet;
|
|
|
|
|
import java.util.Iterator;
|
|
|
|
|
import java.util.Map;
|
|
|
|
|
import java.util.Set;
|
2023-01-28 17:55:51 -08:00
|
|
|
|
2014-04-29 10:54:20 +10:00
|
|
|
import net.minecraft.entity.player.EntityPlayer;
|
|
|
|
|
import net.minecraft.entity.player.EntityPlayerMP;
|
|
|
|
|
import net.minecraft.init.Blocks;
|
|
|
|
|
import net.minecraft.item.ItemStack;
|
|
|
|
|
import net.minecraft.nbt.JsonToNBT;
|
|
|
|
|
import net.minecraft.nbt.NBTTagCompound;
|
|
|
|
|
import net.minecraft.server.MinecraftServer;
|
|
|
|
|
import net.minecraft.world.World;
|
|
|
|
|
import net.minecraftforge.common.DimensionManager;
|
2023-01-28 17:55:51 -08:00
|
|
|
|
2023-03-05 13:11:45 +00:00
|
|
|
import org.apache.commons.io.FileUtils;
|
2014-04-29 10:54:20 +10:00
|
|
|
import org.apache.commons.io.IOUtils;
|
|
|
|
|
import org.apache.logging.log4j.LogManager;
|
|
|
|
|
import org.apache.logging.log4j.Logger;
|
|
|
|
|
|
|
|
|
|
import codechicken.core.CommonUtils;
|
|
|
|
|
import codechicken.core.ServerUtils;
|
|
|
|
|
import codechicken.lib.config.ConfigFile;
|
|
|
|
|
import codechicken.lib.inventory.InventoryUtils;
|
|
|
|
|
import codechicken.lib.packet.PacketCustom;
|
2023-01-28 17:55:51 -08:00
|
|
|
|
2022-06-24 12:32:13 +01:00
|
|
|
public class NEIServerConfig {
|
2023-01-28 17:55:51 -08:00
|
|
|
|
2014-04-29 10:54:20 +10:00
|
|
|
private static MinecraftServer server;
|
|
|
|
|
|
|
|
|
|
public static Logger logger = LogManager.getLogger("NotEnoughItems");
|
|
|
|
|
public static File saveDir;
|
|
|
|
|
public static ConfigFile serverConfig;
|
2019-12-30 20:19:16 -08:00
|
|
|
public static Map<Integer, NBTTagCompound> dimTags = new HashMap<>();
|
|
|
|
|
public static HashMap<String, PlayerSave> playerSaves = new HashMap<>();
|
|
|
|
|
public static ItemStackMap<Set<String>> bannedItems = new ItemStackMap<>();
|
2014-04-29 10:54:20 +10:00
|
|
|
|
|
|
|
|
public static void load(World world) {
|
|
|
|
|
if (MinecraftServer.getServer() != server) {
|
|
|
|
|
logger.debug("Loading NEI Server");
|
|
|
|
|
server = MinecraftServer.getServer();
|
|
|
|
|
saveDir = new File(DimensionManager.getCurrentSaveRootDirectory(), "NEI");
|
|
|
|
|
|
|
|
|
|
dimTags.clear();
|
|
|
|
|
loadConfig();
|
|
|
|
|
loadBannedItems();
|
|
|
|
|
}
|
|
|
|
|
loadWorld(world);
|
|
|
|
|
}
|
|
|
|
|
|
2014-12-03 13:18:44 +10:00
|
|
|
public static File getSaveDir(World world) {
|
|
|
|
|
return new File(CommonUtils.getSaveLocation(world), "NEI");
|
2014-04-29 10:54:20 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static void loadWorld(World world) {
|
|
|
|
|
try {
|
2014-12-03 13:18:44 +10:00
|
|
|
File file = new File(getSaveDir(world), "world.dat");
|
2014-10-01 20:01:10 +10:00
|
|
|
NBTTagCompound tag = NEIServerUtils.readNBT(file);
|
2022-06-24 12:32:13 +01:00
|
|
|
if (tag == null) tag = new NBTTagCompound();
|
2014-04-29 10:54:20 +10:00
|
|
|
dimTags.put(CommonUtils.getDimension(world), tag);
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
throw new RuntimeException(e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static void loadConfig() {
|
|
|
|
|
File file = new File(saveDir, "server.cfg");
|
|
|
|
|
serverConfig = new ConfigFile(file);
|
|
|
|
|
|
|
|
|
|
serverConfig.setNewLineMode(1);
|
|
|
|
|
serverConfig.getTag("permissions").useBraces();
|
2022-06-24 12:32:13 +01:00
|
|
|
serverConfig.getTag("permissions")
|
|
|
|
|
.setComment("List of players who can use these features.\nEg. time=CodeChicken, Friend1");
|
2014-04-29 10:54:20 +10:00
|
|
|
|
|
|
|
|
setDefaultFeature("time");
|
|
|
|
|
setDefaultFeature("rain");
|
|
|
|
|
setDefaultFeature("heal");
|
|
|
|
|
setDefaultFeature("magnet");
|
|
|
|
|
setDefaultFeature("creative");
|
|
|
|
|
setDefaultFeature("creative+");
|
|
|
|
|
setDefaultFeature("adventure");
|
|
|
|
|
setDefaultFeature("enchant");
|
|
|
|
|
setDefaultFeature("potion");
|
|
|
|
|
setDefaultFeature("save-state");
|
|
|
|
|
setDefaultFeature("item");
|
|
|
|
|
setDefaultFeature("delete");
|
|
|
|
|
setDefaultFeature("notify-item", "CONSOLE, OP");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static void setDefaultFeature(String featurename, String... names) {
|
2022-06-24 12:32:13 +01:00
|
|
|
if (names.length == 0) names = new String[] { "OP" };
|
2014-04-29 10:54:20 +10:00
|
|
|
|
2019-12-30 20:19:16 -08:00
|
|
|
StringBuilder list = new StringBuilder();
|
2014-04-29 10:54:20 +10:00
|
|
|
for (int i = 0; i < names.length; i++) {
|
2022-06-24 12:32:13 +01:00
|
|
|
if (i >= 1) list.append(", ");
|
2019-12-30 20:19:16 -08:00
|
|
|
list.append(names[i]);
|
2014-04-29 10:54:20 +10:00
|
|
|
}
|
2019-12-30 20:19:16 -08:00
|
|
|
serverConfig.getTag("permissions." + featurename).setDefaultValue(list.toString());
|
2014-04-29 10:54:20 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static void saveWorld(int dim) {
|
|
|
|
|
try {
|
2023-03-05 13:11:45 +00:00
|
|
|
final File saveDir = getSaveDir(DimensionManager.getWorld(dim));
|
|
|
|
|
if (!saveDir.isDirectory()) {
|
|
|
|
|
FileUtils.forceMkdir(saveDir);
|
|
|
|
|
}
|
|
|
|
|
File file = new File(saveDir, "world.dat");
|
2014-10-01 20:01:10 +10:00
|
|
|
NEIServerUtils.writeNBT(dimTags.get(dim), file);
|
2014-04-29 10:54:20 +10:00
|
|
|
} catch (Exception e) {
|
|
|
|
|
throw new RuntimeException(e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static boolean canPlayerPerformAction(String playername, String name) {
|
|
|
|
|
return isPlayerInList(playername, getPlayerList("permissions." + NEIActions.base(name)), true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static boolean isPlayerInList(String playername, Set<String> list, boolean allowCards) {
|
2022-06-24 12:32:13 +01:00
|
|
|
if (playername.equals("CONSOLE")) return list.contains(playername);
|
2014-04-29 10:54:20 +10:00
|
|
|
|
|
|
|
|
playername = playername.toLowerCase();
|
|
|
|
|
|
|
|
|
|
if (allowCards) {
|
2022-06-24 12:32:13 +01:00
|
|
|
if (list.contains("ALL")) return true;
|
2014-04-29 10:54:20 +10:00
|
|
|
if ((ServerUtils.isPlayerOP(playername) || ServerUtils.isPlayerOwner(playername)) && list.contains("OP"))
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return list.contains(playername);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static boolean isActionDisabled(int dim, String name) {
|
|
|
|
|
return dimTags.get(dim).getBoolean("disabled" + name);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void disableAction(int dim, String name, boolean disable) {
|
|
|
|
|
dimTags.get(dim).setBoolean("disabled" + name, disable);
|
|
|
|
|
NEISPH.sendActionDisabled(dim, name, disable);
|
|
|
|
|
saveWorld(dim);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static HashSet<String> getPlayerList(String tag) {
|
|
|
|
|
String[] list = serverConfig.getTag(tag).getValue("").replace(" ", "").split(",");
|
2019-12-30 20:19:16 -08:00
|
|
|
return new HashSet<>(Arrays.asList(list));
|
2014-04-29 10:54:20 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void addPlayerToList(String playername, String tag) {
|
|
|
|
|
HashSet<String> list = getPlayerList(tag);
|
|
|
|
|
|
|
|
|
|
if (!playername.equals("CONSOLE") && !playername.equals("ALL") && !playername.equals("OP"))
|
|
|
|
|
playername = playername.toLowerCase();
|
|
|
|
|
|
|
|
|
|
list.add(playername);
|
|
|
|
|
savePlayerList(tag, list);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void remPlayerFromList(String playername, String tag) {
|
|
|
|
|
HashSet<String> list = getPlayerList(tag);
|
|
|
|
|
|
|
|
|
|
if (!playername.equals("CONSOLE") && !playername.equals("ALL") && !playername.equals("OP"))
|
|
|
|
|
playername = playername.toLowerCase();
|
|
|
|
|
|
|
|
|
|
list.remove(playername);
|
|
|
|
|
savePlayerList(tag, list);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static void savePlayerList(String tag, Collection<String> list) {
|
|
|
|
|
StringBuilder sb = new StringBuilder();
|
|
|
|
|
int i = 0;
|
|
|
|
|
for (Iterator<String> iterator = list.iterator(); iterator.hasNext(); i++) {
|
2022-06-24 12:32:13 +01:00
|
|
|
if (i != 0) sb.append(", ");
|
2014-04-29 10:54:20 +10:00
|
|
|
|
|
|
|
|
sb.append(iterator.next());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
serverConfig.getTag(tag).setValue(sb.toString());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static void loadBannedItems() {
|
|
|
|
|
bannedItems.clear();
|
|
|
|
|
File file = new File(saveDir, "banneditems.cfg");
|
2022-06-24 12:32:13 +01:00
|
|
|
if (!file.exists()) {
|
2019-12-30 20:19:16 -08:00
|
|
|
bannedItems.put(new ItemStack(Blocks.command_block), new HashSet<>(Collections.singletonList("NONE")));
|
2014-04-29 10:54:20 +10:00
|
|
|
saveBannedItems();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
try {
|
|
|
|
|
FileReader r = new FileReader(file);
|
|
|
|
|
int line = 0;
|
2022-06-24 12:32:13 +01:00
|
|
|
for (String s : IOUtils.readLines(r)) {
|
|
|
|
|
if (s.charAt(0) == '#' || s.trim().length() == 0) continue;
|
2014-04-29 10:54:20 +10:00
|
|
|
int delim = s.lastIndexOf('=');
|
2022-06-24 12:32:13 +01:00
|
|
|
if (delim < 0) {
|
|
|
|
|
logger.error("line " + line + ": Missing =");
|
2014-04-29 10:54:20 +10:00
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
try {
|
|
|
|
|
NBTTagCompound key = (NBTTagCompound) JsonToNBT.func_150315_a(s.substring(0, delim));
|
2019-12-30 20:19:16 -08:00
|
|
|
Set<String> values = new HashSet<>();
|
2022-06-24 12:32:13 +01:00
|
|
|
for (String s2 : s.substring(delim + 1).split(",")) values.add(s2.trim());
|
2014-04-29 10:54:20 +10:00
|
|
|
bannedItems.put(InventoryUtils.loadPersistant(key), values);
|
|
|
|
|
} catch (Exception e) {
|
2022-06-24 12:32:13 +01:00
|
|
|
logger.error("line " + line + ": " + e.getMessage());
|
2014-04-29 10:54:20 +10:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
r.close();
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
throw new RuntimeException(e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void saveBannedItems() {
|
|
|
|
|
File file = new File(saveDir, "banneditems.cfg");
|
|
|
|
|
try {
|
2022-06-24 12:32:13 +01:00
|
|
|
if (!file.exists()) file.createNewFile();
|
2014-04-29 10:54:20 +10:00
|
|
|
|
|
|
|
|
PrintWriter p = new PrintWriter(file);
|
2022-06-24 12:32:13 +01:00
|
|
|
p.println(
|
|
|
|
|
"#Saved in this format for external editing. The format isn't that hard to figure out. If you think you're up to it, modify it here!");
|
2014-04-29 10:54:20 +10:00
|
|
|
|
|
|
|
|
for (ItemStackMap.Entry<Set<String>> entry : bannedItems.entries()) {
|
|
|
|
|
NBTTagCompound key = InventoryUtils.savePersistant(entry.key, new NBTTagCompound());
|
|
|
|
|
key.removeTag("Count");
|
2022-06-24 12:32:13 +01:00
|
|
|
if (key.getByte("Damage") == 0) key.removeTag("Damage");
|
2014-04-29 10:54:20 +10:00
|
|
|
|
|
|
|
|
p.print(key.toString());
|
|
|
|
|
p.print("=[");
|
|
|
|
|
int i = 0;
|
|
|
|
|
for (String s : entry.value) {
|
2022-06-24 12:32:13 +01:00
|
|
|
if (i++ != 0) p.print(", ");
|
2014-04-29 10:54:20 +10:00
|
|
|
p.print(s);
|
|
|
|
|
}
|
|
|
|
|
p.println("]");
|
|
|
|
|
}
|
|
|
|
|
p.close();
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
throw new RuntimeException(e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static PlayerSave forPlayer(String username) {
|
|
|
|
|
return playerSaves.get(username);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void loadPlayer(EntityPlayer player) {
|
|
|
|
|
logger.debug("Loading Player: " + player.getGameProfile().getName());
|
2022-06-24 12:32:13 +01:00
|
|
|
playerSaves.put(
|
|
|
|
|
player.getCommandSenderName(),
|
|
|
|
|
new PlayerSave(player.getCommandSenderName(), new File(saveDir, "players")));
|
2014-04-29 10:54:20 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void unloadPlayer(EntityPlayer player) {
|
|
|
|
|
logger.debug("Unloading Player: " + player.getCommandSenderName());
|
|
|
|
|
PlayerSave playerSave = playerSaves.remove(player.getCommandSenderName());
|
2022-06-24 12:32:13 +01:00
|
|
|
if (playerSave != null) playerSave.save();
|
2014-04-29 10:54:20 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static boolean authenticatePacket(EntityPlayerMP sender, PacketCustom packet) {
|
|
|
|
|
switch (packet.getType()) {
|
|
|
|
|
case 1:
|
|
|
|
|
return canPlayerPerformAction(sender.getCommandSenderName(), "item");
|
|
|
|
|
case 4:
|
|
|
|
|
return canPlayerPerformAction(sender.getCommandSenderName(), "delete");
|
|
|
|
|
case 6:
|
|
|
|
|
return canPlayerPerformAction(sender.getCommandSenderName(), "magnet");
|
|
|
|
|
case 7:
|
|
|
|
|
return canPlayerPerformAction(sender.getCommandSenderName(), "time");
|
|
|
|
|
case 8:
|
|
|
|
|
return canPlayerPerformAction(sender.getCommandSenderName(), "heal");
|
|
|
|
|
case 9:
|
|
|
|
|
return canPlayerPerformAction(sender.getCommandSenderName(), "rain");
|
|
|
|
|
case 14:
|
|
|
|
|
case 23:
|
|
|
|
|
return canPlayerPerformAction(sender.getCommandSenderName(), "creative+");
|
|
|
|
|
case 21:
|
|
|
|
|
case 22:
|
|
|
|
|
return canPlayerPerformAction(sender.getCommandSenderName(), "enchant");
|
|
|
|
|
case 24:
|
|
|
|
|
return canPlayerPerformAction(sender.getCommandSenderName(), "potion");
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|