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;
}
/*
* 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() {
return table;
}

View File

@ -9,5 +9,9 @@ public interface BlockType {
// 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();
}

View File

@ -1,8 +1,11 @@
package net.Indyuce.mmocore.api.block;
import org.apache.commons.lang.Validate;
import org.bukkit.Location;
import org.bukkit.Material;
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.mmogroup.mmolib.api.MMOLineConfig;
@ -10,14 +13,24 @@ import net.mmogroup.mmolib.api.MMOLineConfig;
public class VanillaBlockType implements BlockType {
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) {
config.validate("type");
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) {
type = block.getType();
age = 0;
}
public Material getType() {
@ -27,6 +40,12 @@ public class VanillaBlockType implements BlockType {
@Override
public void place(Location loc, RegeneratingBlock block) {
loc.getBlock().setType(type);
BlockData state = loc.getBlock().getBlockData();
if (age > 0 && state instanceof Ageable) {
((Ageable) state).setAge(age);
loc.getBlock().setBlockData(state);
}
}
@Override

View File

@ -1,16 +1,16 @@
package net.Indyuce.mmocore.api.load;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.ConfigurationSection;
public abstract class PostLoadObject {
private FileConfiguration config;
private ConfigurationSection config;
/*
* objects which must load some data afterwards, like quests which must load
* their parent quests after all quests were initialized or classes which
* must load their subclasses
*/
public PostLoadObject(FileConfiguration config) {
public PostLoadObject(ConfigurationSection config) {
this.config = config;
}
@ -23,5 +23,5 @@ public abstract class PostLoadObject {
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.Material;
import org.bukkit.Particle;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
@ -182,7 +183,7 @@ public class PlayerClass extends PostLoadObject {
}
@Override
protected void whenPostLoaded(FileConfiguration config) {
protected void whenPostLoaded(ConfigurationSection config) {
if (config.contains("subclasses"))
for (String key : config.getConfigurationSection("subclasses").getKeys(false))
try {

View File

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

View File

@ -42,7 +42,7 @@ public class BlockListener implements Listener {
boolean customMine = MMOCore.plugin.mineManager.isEnabled(player, block.getLocation());
if (!customMine)
return;
BlockInfo info = MMOCore.plugin.mineManager.getInfo(block);
if (info == null) {
event.setCancelled(true);
@ -104,7 +104,8 @@ public class BlockListener implements Listener {
* apply drop tables
*/
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())
if (drop.getType() != Material.AIR && drop.getAmount() > 0)
block.getWorld().dropItemNaturally(dropLocation, drop);

View File

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

View File

@ -6,18 +6,18 @@ import java.util.Map;
import java.util.Set;
import java.util.logging.Level;
import org.apache.commons.lang.Validate;
import org.bukkit.Material;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.file.FileConfiguration;
import net.Indyuce.mmocore.MMOCore;
import net.Indyuce.mmocore.api.block.BlockType;
import net.Indyuce.mmocore.api.load.PostLoadObject;
import net.mmogroup.mmolib.api.MMOLineConfig;
public class RestrictionManager {
// 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) {
@ -30,18 +30,17 @@ public class RestrictionManager {
for (BlockPermissions perms : map.values())
try {
perms.load();
perms.postLoad();
} catch (IllegalArgumentException exception) {
MMOCore.log(Level.WARNING, "Could not load block perms " + perms.getTool().name() + ": " + exception.getMessage());
}
}
public void register(BlockPermissions perms) {
if (perms.isValid()) {
if (perms.isValid())
map.put(perms.getTool(), perms);
// perms.getMinable().forEach(material ->
// breakBlackList.add(material));
}
// perms.getMinable().forEach(material ->
// breakBlackList.add(material));
}
// public boolean isBlackListed(String s) {
@ -49,39 +48,27 @@ public class RestrictionManager {
// }
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 Material tool;
private BlockPermissions parent;
/*
* cache configuration section for easier laod
*/
private ConfigurationSection loaded;
public BlockPermissions(ConfigurationSection config) {
super(config);
/*
* 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());
tool = Material.valueOf(config.getName());
}
public void load() {
if (loaded.contains("parent"))
parent = map.get(Material.valueOf(loaded.getString("parent", "???").toUpperCase().replace("-", "_").replace(" ", "_")));
for (String key : loaded.getStringList("can-mine"))
@Override
protected void whenPostLoaded(ConfigurationSection config) {
if (config.contains("parent"))
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)));
loaded = null;
}
public void addPermission(BlockType block) {