!Block type refactor, moving libs over to MMOLib

This commit is contained in:
Indyuce 2020-04-04 15:01:38 +02:00
parent ed419d7ce8
commit ac7f0ba63c
8 changed files with 71 additions and 119 deletions

Binary file not shown.

Binary file not shown.

View File

@ -20,12 +20,13 @@ import net.Indyuce.mmoitems.api.player.PlayerData;
public class CraftingStation {
private final String id, name;
private StationItemOptions itemOptions;
private int maxQueueSize;
private Map<String, Recipe> recipes = new LinkedHashMap<>();
private final StationItemOptions itemOptions;
private final int maxQueueSize;
private final Map<String, Recipe> recipes = new LinkedHashMap<>();
public CraftingStation(String id, FileConfiguration config) {
this(id.toLowerCase().replace("_", "-").replace(" ", "-"), config.getString("name"));
this.id = id.toLowerCase().replace("_", "-").replace(" ", "-");
this.name = ChatColor.translateAlternateColorCodes('&', config.getString("name"));
for (String key : config.getConfigurationSection("recipes").getKeys(false))
registerRecipe(loadRecipe(config.getConfigurationSection("recipes." + key)));
@ -34,12 +35,14 @@ public class CraftingStation {
maxQueueSize = Math.max(1, Math.min(config.getInt("max-queue-size"), 64));
}
public CraftingStation(String id, String name) {
public CraftingStation(String id, String name, StationItemOptions itemOptions, int maxQueueSize) {
Validate.notNull(id, "Crafting station ID must not be null");
Validate.notNull(name, "Crafting station name must not be null");
this.id = id;
this.id = id.toLowerCase().replace("_", "-").replace(" ", "-");
this.name = ChatColor.translateAlternateColorCodes('&', name);
this.itemOptions = itemOptions;
this.maxQueueSize = maxQueueSize;
}
public String getId() {

View File

@ -1,105 +0,0 @@
package net.Indyuce.mmoitems.api.util;
import org.apache.commons.lang.Validate;
import com.google.gson.JsonObject;
import com.google.gson.JsonParseException;
import com.google.gson.JsonParser;
public class MMOLineConfig {
private final String key, value;
private final String[] args;
private final JsonObject json;
public MMOLineConfig(String value) {
this.value = value;
/*
* if there is no config, no need to parse the json object. split,
* define key and find arg
*/
if (!value.contains("{") || !value.contains("}")) {
String[] split = value.split("\\ ");
key = split[0];
args = split.length > 1 ? value.replace(key + " ", "").split("\\ ") : new String[0];
json = new JsonObject();
return;
}
/*
* load json and extra args
*/
try {
int begin = value.indexOf("{"), end = value.lastIndexOf("}") + 1;
key = value.substring(0, begin);
json = new JsonParser().parse(value.substring(begin, end)).getAsJsonObject();
String format = value.substring(Math.min(value.length(), end + 1));
args = format.isEmpty() ? new String[0] : format.split("\\ ");
} catch (JsonParseException exception) {
throw new IllegalArgumentException("Could not load config");
}
}
/*
* extra arguments outside the config brackets
*/
public String[] args() {
return args;
}
public String getKey() {
return key;
}
public String getString(String path) {
return json.get(path).getAsString();
}
public String getString(String path, String def) {
return json.has(path) ? getString(path) : def;
}
public double getDouble(String path) {
return json.get(path).getAsDouble();
}
public int getInt(String path) {
return json.get(path).getAsInt();
}
public int getInt(String path, int def) {
return json.has(path) ? getInt(path) : def;
}
public long getLong(String path) {
return json.get(path).getAsLong();
}
public boolean getBoolean(String path) {
return json.get(path).getAsBoolean();
}
public boolean getBoolean(String path, boolean def) {
return json.has(path) ? getBoolean(path) : def;
}
public boolean contains(String path) {
return json.has(path);
}
public void validate(String... paths) {
for (String path : paths)
Validate.isTrue(contains(path), "Config is missing parameter '" + path + "'");
}
public void validateArgs(int count) {
Validate.isTrue(args.length >= count, "Config must have at least " + count + " parameters");
}
@Override
public String toString() {
return value;
}
}

View File

@ -3,6 +3,7 @@ package net.Indyuce.mmoitems.comp.mmocore;
import org.bukkit.configuration.ConfigurationSection;
import net.Indyuce.mmocore.MMOCore;
import net.Indyuce.mmocore.api.block.BlockType;
import net.Indyuce.mmocore.api.droptable.condition.Condition;
import net.Indyuce.mmocore.api.droptable.dropitem.DropItem;
import net.Indyuce.mmocore.api.experience.Profession;
@ -19,10 +20,11 @@ import net.Indyuce.mmoitems.comp.mmocore.crafting.ProfessionCondition;
import net.Indyuce.mmoitems.comp.mmocore.load.GetMMOItemObjective;
import net.Indyuce.mmoitems.comp.mmocore.load.MMOItemDropItem;
import net.Indyuce.mmoitems.comp.mmocore.load.MMOItemTrigger;
import net.Indyuce.mmoitems.comp.mmocore.load.MMOItemsBlockType;
import net.Indyuce.mmoitems.comp.mmocore.load.MineMIBlockExperienceSource;
import net.Indyuce.mmoitems.comp.mmocore.load.SmeltMMOItemExperienceSource;
public class MMOCoreMMOLoader implements MMOLoader {
public class MMOCoreMMOLoader extends MMOLoader {
/*
* called when MMOItems loads
@ -81,4 +83,14 @@ public class MMOCoreMMOLoader implements MMOLoader {
return null;
}
@Override
public BlockType loadBlockType(MMOLineConfig config) {
if (config.getKey().equalsIgnoreCase("miblock") || config.getKey().equals("mmoitemsblock")|| config.getKey().equals("mmoitem")|| config.getKey().equals("mmoitems"))
return new MMOItemsBlockType(config);
return null;
}
}

View File

@ -0,0 +1,46 @@
package net.Indyuce.mmoitems.comp.mmocore.load;
import org.bukkit.Location;
import org.bukkit.block.Block;
import net.Indyuce.mmocore.api.block.BlockInfo.RegeneratingBlock;
import net.Indyuce.mmocore.api.block.BlockType;
import net.Indyuce.mmocore.api.load.MMOLineConfig;
import net.Indyuce.mmoitems.MMOItems;
import net.Indyuce.mmoitems.api.CustomBlock;
import net.mmogroup.mmolib.MMOLib;
public class MMOItemsBlockType implements BlockType {
private final int id;
public MMOItemsBlockType(MMOLineConfig config) {
config.validate("id");
id = config.getInt("id");
}
public MMOItemsBlockType(Block block) {
id = CustomBlock.getFromData(block.getBlockData()).getId();
}
public int getBlockId() {
return id;
}
@Override
public void place(Location loc, RegeneratingBlock regenerating) {
CustomBlock block = MMOItems.plugin.getCustomBlocks().getBlock(id);
loc.getBlock().setType(block.getType());
if (MMOLib.plugin.getVersion().isStrictlyHigher(1, 12))
loc.getBlock().setBlockData(block.getBlockData());
}
public static boolean matches(Block block) {
return MMOItems.plugin.getCustomBlocks().isMushroomBlock(block.getType());
}
@Override
public String generateKey() {
return "mmoitems-custom-block-" + id;
}
}

View File

@ -32,11 +32,7 @@ public class BlockManager {
}
public CustomBlock getBlock(int id) {
if (id > 0 && id < 161 && id != 54)
return customBlocks.get(id);
MMOItems.plugin.getLogger().warning("Invalid CustomBlock Id! " + id);
return null;
return id > 0 && id < 161 && id != 54 ? customBlocks.get(id) : null;
}
public CustomBlock getBlock(MushroomState state) {
@ -48,7 +44,7 @@ public class BlockManager {
FileConfiguration config = new ConfigFile("custom-blocks").getConfig();
for (int id = 1; id < 161; id++)
if (id != 54)
if (id != 54 && config.contains("" + id))
try {
MushroomState state = new MushroomState(getType(id), upIds.contains(id), downIds.contains(id), westIds.contains(id), eastIds.contains(id), southIds.contains(id), northIds.contains(id));
customBlocks.put(id, new CustomBlock(id, state, config.getConfigurationSection("" + id)));

View File

@ -33,7 +33,7 @@ import net.Indyuce.mmoitems.api.crafting.ingredient.MMOItemIngredient;
import net.Indyuce.mmoitems.api.crafting.ingredient.VanillaIngredient;
import net.Indyuce.mmoitems.api.crafting.trigger.Trigger;
import net.Indyuce.mmoitems.api.util.AltChar;
import net.Indyuce.mmoitems.api.util.MMOLineConfig;
import net.mmogroup.mmolib.api.MMOLineConfig;
import net.mmogroup.mmolib.api.item.NBTItem;
public class CraftingManager {