Added 'age' option to vanilla block type

This commit is contained in:
Indyuce 2020-04-28 18:56:47 +02:00
parent f44da7456a
commit f09d1fe7c3
9 changed files with 50 additions and 46 deletions

View File

@ -65,14 +65,6 @@ public class BlockInfo {
return block; return block;
} }
/*
* generates a key used to store the BlockInfo instance in the manager map,
* the key depends on the block type to make sure there is no interference
*/
public String generateKey() {
return block.generateKey();
}
public DropTable getDropTable() { public DropTable getDropTable() {
return table; return table;
} }

View File

@ -9,5 +9,9 @@ public interface BlockType {
// public boolean matches(Block block); // public boolean matches(Block block);
/*
* generates a key used to store the BlockInfo instance in the manager map,
* the key depends on the block type to make sure there is no interference
*/
public String generateKey(); public String generateKey();
} }

View File

@ -1,8 +1,11 @@
package net.Indyuce.mmocore.api.block; package net.Indyuce.mmocore.api.block;
import org.apache.commons.lang.Validate;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.block.Block; import org.bukkit.block.Block;
import org.bukkit.block.data.Ageable;
import org.bukkit.block.data.BlockData;
import net.Indyuce.mmocore.api.block.BlockInfo.RegeneratingBlock; import net.Indyuce.mmocore.api.block.BlockInfo.RegeneratingBlock;
import net.mmogroup.mmolib.api.MMOLineConfig; import net.mmogroup.mmolib.api.MMOLineConfig;
@ -10,14 +13,24 @@ import net.mmogroup.mmolib.api.MMOLineConfig;
public class VanillaBlockType implements BlockType { public class VanillaBlockType implements BlockType {
private final Material type; private final Material type;
/*
* allows to plant back crops with a custom age so that it does not always
* have to full grow again
*/
private final int age;
public VanillaBlockType(MMOLineConfig config) { public VanillaBlockType(MMOLineConfig config) {
config.validate("type"); config.validate("type");
type = Material.valueOf(config.getString("type").toUpperCase().replace("-", "_").replace(" ", "_")); type = Material.valueOf(config.getString("type").toUpperCase().replace("-", "_").replace(" ", "_"));
age = config.getInt("age", 0);
Validate.isTrue(age >= 0 && age < 8, "Age must be between 0 and 7");
} }
public VanillaBlockType(Block block) { public VanillaBlockType(Block block) {
type = block.getType(); type = block.getType();
age = 0;
} }
public Material getType() { public Material getType() {
@ -27,6 +40,12 @@ public class VanillaBlockType implements BlockType {
@Override @Override
public void place(Location loc, RegeneratingBlock block) { public void place(Location loc, RegeneratingBlock block) {
loc.getBlock().setType(type); loc.getBlock().setType(type);
BlockData state = loc.getBlock().getBlockData();
if (age > 0 && state instanceof Ageable) {
((Ageable) state).setAge(age);
loc.getBlock().setBlockData(state);
}
} }
@Override @Override

View File

@ -1,16 +1,16 @@
package net.Indyuce.mmocore.api.load; package net.Indyuce.mmocore.api.load;
import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.configuration.ConfigurationSection;
public abstract class PostLoadObject { public abstract class PostLoadObject {
private FileConfiguration config; private ConfigurationSection config;
/* /*
* objects which must load some data afterwards, like quests which must load * objects which must load some data afterwards, like quests which must load
* their parent quests after all quests were initialized or classes which * their parent quests after all quests were initialized or classes which
* must load their subclasses * must load their subclasses
*/ */
public PostLoadObject(FileConfiguration config) { public PostLoadObject(ConfigurationSection config) {
this.config = config; this.config = config;
} }
@ -23,5 +23,5 @@ public abstract class PostLoadObject {
config = null; config = null;
} }
protected abstract void whenPostLoaded(FileConfiguration config); protected abstract void whenPostLoaded(ConfigurationSection config);
} }

View File

@ -15,6 +15,7 @@ import org.apache.commons.lang.Validate;
import org.bukkit.ChatColor; import org.bukkit.ChatColor;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.Particle; import org.bukkit.Particle;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta; import org.bukkit.inventory.meta.ItemMeta;
@ -182,7 +183,7 @@ public class PlayerClass extends PostLoadObject {
} }
@Override @Override
protected void whenPostLoaded(FileConfiguration config) { protected void whenPostLoaded(ConfigurationSection config) {
if (config.contains("subclasses")) if (config.contains("subclasses"))
for (String key : config.getConfigurationSection("subclasses").getKeys(false)) for (String key : config.getConfigurationSection("subclasses").getKeys(false))
try { try {

View File

@ -70,7 +70,7 @@ public class Quest extends PostLoadObject {
} }
@Override @Override
protected void whenPostLoaded(FileConfiguration config) { protected void whenPostLoaded(ConfigurationSection config) {
if (config.contains("parent")) if (config.contains("parent"))
for (String parent : config.getStringList("parent")) for (String parent : config.getStringList("parent"))
try { try {

View File

@ -104,7 +104,8 @@ public class BlockListener implements Listener {
* apply drop tables * apply drop tables
*/ */
if (info.hasDropTable()) { if (info.hasDropTable()) {
Location dropLocation = getSafeDropLocation(block, !block.getType().isSolid() || !(info.regenerates() && info.getRegenerationInfo().hasTemporaryBlock())); Location dropLocation = getSafeDropLocation(block,
!block.getType().isSolid() || !(info.regenerates() && info.getRegenerationInfo().hasTemporaryBlock()));
for (ItemStack drop : called.getDrops()) for (ItemStack drop : called.getDrops())
if (drop.getType() != Material.AIR && drop.getAmount() > 0) if (drop.getType() != Material.AIR && drop.getAmount() > 0)
block.getWorld().dropItemNaturally(dropLocation, drop); block.getWorld().dropItemNaturally(dropLocation, drop);

View File

@ -59,7 +59,7 @@ public class CustomBlockManager extends MMOManager {
} }
public void register(BlockInfo regen) { public void register(BlockInfo regen) {
map.put(regen.generateKey(), regen); map.put(regen.getBlock().generateKey(), regen);
} }
public BlockInfo getInfo(Block block) { public BlockInfo getInfo(Block block) {

View File

@ -6,18 +6,18 @@ import java.util.Map;
import java.util.Set; import java.util.Set;
import java.util.logging.Level; import java.util.logging.Level;
import org.apache.commons.lang.Validate;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.configuration.ConfigurationSection; import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.configuration.file.FileConfiguration;
import net.Indyuce.mmocore.MMOCore; import net.Indyuce.mmocore.MMOCore;
import net.Indyuce.mmocore.api.block.BlockType; import net.Indyuce.mmocore.api.block.BlockType;
import net.Indyuce.mmocore.api.load.PostLoadObject;
import net.mmogroup.mmolib.api.MMOLineConfig; import net.mmogroup.mmolib.api.MMOLineConfig;
public class RestrictionManager { public class RestrictionManager {
// private Set<String> breakBlackList = new HashSet<>(); // private Set<String> breakBlackList = new HashSet<>();
private Map<Material, BlockPermissions> map = new HashMap<>(); private final Map<Material, BlockPermissions> map = new HashMap<>();
public RestrictionManager(FileConfiguration config) { public RestrictionManager(FileConfiguration config) {
@ -30,58 +30,45 @@ public class RestrictionManager {
for (BlockPermissions perms : map.values()) for (BlockPermissions perms : map.values())
try { try {
perms.load(); perms.postLoad();
} catch (IllegalArgumentException exception) { } catch (IllegalArgumentException exception) {
MMOCore.log(Level.WARNING, "Could not load block perms " + perms.getTool().name() + ": " + exception.getMessage()); MMOCore.log(Level.WARNING, "Could not load block perms " + perms.getTool().name() + ": " + exception.getMessage());
} }
} }
public void register(BlockPermissions perms) { public void register(BlockPermissions perms) {
if (perms.isValid()) { if (perms.isValid())
map.put(perms.getTool(), perms); map.put(perms.getTool(), perms);
// perms.getMinable().forEach(material -> // perms.getMinable().forEach(material ->
// breakBlackList.add(material)); // breakBlackList.add(material));
} }
}
// public boolean isBlackListed(String s) { // public boolean isBlackListed(String s) {
// return breakBlackList.contains(s); // return breakBlackList.contains(s);
// } // }
public BlockPermissions getPermissions(Material tool) { public BlockPermissions getPermissions(Material tool) {
return map.containsKey(tool) ? map.get(tool) : null; return map.getOrDefault(tool, null);
} }
public class BlockPermissions { public class BlockPermissions extends PostLoadObject {
private final Set<BlockType> mineable = new HashSet<>(); private final Set<BlockType> mineable = new HashSet<>();
private final Material tool; private final Material tool;
private BlockPermissions parent; private BlockPermissions parent;
/* public BlockPermissions(ConfigurationSection config) {
* cache configuration section for easier laod super(config);
*/
private ConfigurationSection loaded;
/* tool = Material.valueOf(config.getName());
* these instances must be initialized before loading data about them
* because in order to load PARENT permissions, every instance needs to
* be added to the map first
*/
public BlockPermissions(ConfigurationSection section) {
Validate.notNull(section, "Could not load config");
this.loaded = section;
tool = Material.valueOf(section.getName());
} }
public void load() { @Override
if (loaded.contains("parent")) protected void whenPostLoaded(ConfigurationSection config) {
parent = map.get(Material.valueOf(loaded.getString("parent", "???").toUpperCase().replace("-", "_").replace(" ", "_"))); if (config.contains("parent"))
for (String key : loaded.getStringList("can-mine")) parent = map.get(Material.valueOf(config.getString("parent", "None").toUpperCase().replace("-", "_").replace(" ", "_")));
for (String key : config.getStringList("can-mine"))
mineable.add(MMOCore.plugin.loadManager.loadBlockType(new MMOLineConfig(key))); mineable.add(MMOCore.plugin.loadManager.loadBlockType(new MMOLineConfig(key)));
loaded = null;
} }
public void addPermission(BlockType block) { public void addPermission(BlockType block) {