!Small cleanup

This commit is contained in:
Indyuce 2020-12-24 01:08:20 +01:00
parent 0c789c8154
commit 40894d57ad
5 changed files with 64 additions and 48 deletions

View File

@ -5,9 +5,6 @@ import java.util.List;
import java.util.Optional;
import java.util.logging.Level;
import net.mmogroup.mmolib.UtilityMethods;
import net.mmogroup.mmolib.api.condition.BlockCondition;
import net.mmogroup.mmolib.api.condition.MMOCondition;
import org.apache.commons.lang.Validate;
import org.bukkit.Location;
import org.bukkit.block.Block;
@ -20,7 +17,10 @@ import net.Indyuce.mmocore.api.droptable.DropTable;
import net.Indyuce.mmocore.api.loot.LootBuilder;
import net.Indyuce.mmocore.api.quest.trigger.ExperienceTrigger;
import net.Indyuce.mmocore.api.quest.trigger.Trigger;
import net.mmogroup.mmolib.UtilityMethods;
import net.mmogroup.mmolib.api.MMOLineConfig;
import net.mmogroup.mmolib.api.condition.BlockCondition;
import net.mmogroup.mmolib.api.condition.MMOCondition;
public class BlockInfo {
private final BlockType block;
@ -65,7 +65,8 @@ public class BlockInfo {
if (config.isList("conditions"))
for (String key : config.getStringList("conditions")) {
MMOCondition condition = UtilityMethods.getCondition(key);
if(condition instanceof BlockCondition) conditions.add((BlockCondition) condition);
if (condition instanceof BlockCondition)
conditions.add((BlockCondition) condition);
}
}
@ -124,8 +125,8 @@ public class BlockInfo {
public boolean checkConditions(Block block) {
for (BlockCondition condition : conditions)
if(!condition.check(block)) return false;
if (!condition.check(block))
return false;
return true;
}

View File

@ -3,13 +3,13 @@ package net.Indyuce.mmocore.api.event;
import java.util.ArrayList;
import java.util.List;
import net.Indyuce.mmocore.api.droptable.condition.ConditionInstance;
import org.bukkit.block.Block;
import org.bukkit.event.Cancellable;
import org.bukkit.event.HandlerList;
import org.bukkit.inventory.ItemStack;
import net.Indyuce.mmocore.api.block.BlockInfo;
import net.Indyuce.mmocore.api.droptable.condition.ConditionInstance;
import net.Indyuce.mmocore.api.experience.ExperienceInfo;
import net.Indyuce.mmocore.api.loot.LootBuilder;
import net.Indyuce.mmocore.api.player.PlayerData;
@ -22,6 +22,7 @@ public class CustomBlockMineEvent extends PlayerDataEvent implements Cancellable
private final List<ItemStack> drops;
private final ExperienceInfo experience;
@Deprecated
private boolean canBreak;
private boolean cancelled = false;
@ -30,9 +31,9 @@ public class CustomBlockMineEvent extends PlayerDataEvent implements Cancellable
this.block = block;
this.info = info;
this.drops = (info.hasDropTable() && player.isOnline()
&& info.getDropTable().areConditionsMet(new ConditionInstance(player.getPlayer())))
? info.collectDrops(new LootBuilder(player, 0)) : new ArrayList<>();
this.drops = (info.hasDropTable() && player.isOnline() && info.getDropTable().areConditionsMet(new ConditionInstance(player.getPlayer())))
? info.collectDrops(new LootBuilder(player, 0))
: new ArrayList<>();
this.experience = info.hasExperience() ? info.getExperience().newInfo() : null;
this.canBreak = canBreak;
}
@ -57,10 +58,12 @@ public class CustomBlockMineEvent extends PlayerDataEvent implements Cancellable
return experience;
}
@Deprecated
public boolean canBreak() {
return canBreak;
}
@Deprecated
public void setCanBreak(boolean value) {
canBreak = value;
}

View File

@ -12,21 +12,25 @@ import org.apache.commons.lang.Validate;
public class ExpCurve {
private final String id;
/*
* experience needed to level up. different professions or classes can have
/**
* Experience needed to level up. Different professions or classes can have
* different exp curves so that it is easier to balance.
*/
private final List<Integer> experience = new ArrayList<>();
/*
* purely arbitrary but MMOCore needs a default exp curve for everything
/**
* Purely arbitrary but MMOCore needs a default exp curve for everything
* otherwise there might be divisions by 0 when trying to update the vanilla
* exp bar which requires a 0.0 -> 1.0 float as parameter
*/
public static final ExpCurve DEFAULT = new ExpCurve("default", 100, 200, 300, 400, 500);
/*
* read exp curve from a file, one line after the other
/**
* Reads an exp curve from a text file, one line after the other. Each exp
* value has to be the only thing written on every line
*
* @param file Text file to read data from
* @throws IOException IO exception when reading file
*/
public ExpCurve(File file) throws IOException {
this.id = file.getName().replace(".txt", "").toLowerCase().replace("_", "-").replace(" ", "-");
@ -40,9 +44,13 @@ public class ExpCurve {
Validate.isTrue(!experience.isEmpty(), "There must be at least one exp value in your exp curve");
}
/*
* can be used by external plugins to register curves and it used by MMOCore
* to create the default exp curve if none is selected
/**
* Public constructor for external plugins
*
* @param id Some unique identifier to let other plugin features refer
* to your exp curve.
* @param values The exp values, at to be at least one or the constructor
* will throw an error
*/
public ExpCurve(String id, int... values) {
this.id = id;
@ -55,10 +63,11 @@ public class ExpCurve {
return id;
}
/*
* retrieves the experience needed. the level serves as index for the list
* checkup. if the level is higher than the amount of exp inputs, just
* return the last list value
/**
* @param level Level being reached by some player
* @return Experience needed to reach provided level. The level serves
* an index for a list checkup. If the level is higher than
* the list size, it just returns the last value of the list
*/
public int getExperience(int level) {
Validate.isTrue(level > 0, "Level must be stricly positive");

View File

@ -30,6 +30,7 @@ import net.mmogroup.mmolib.UtilityMethods;
public class BlockListener implements Listener {
private static final BlockFace[] order = { BlockFace.UP, BlockFace.DOWN, BlockFace.EAST, BlockFace.NORTH, BlockFace.WEST, BlockFace.SOUTH };
@SuppressWarnings("deprecation")
@EventHandler(priority = EventPriority.HIGH, ignoreCancelled = true)
public void onCustomBreak(BlockBreakEvent event) {
Player player = event.getPlayer();
@ -47,7 +48,8 @@ public class BlockListener implements Listener {
return;
/*
* If the block is a temporary block, immediately cancel the break event
* If the block is a temporary block placed by block regen, immediately
* cancel the break event
*/
BlockInfo info = MMOCore.plugin.mineManager.getInfo(block);
boolean temporaryBlock = MMOCore.plugin.mineManager.isTemporaryBlock(block);
@ -56,9 +58,6 @@ public class BlockListener implements Listener {
return;
}
/*
* Check if the block has exp or drop tables
*/
if (info == null) {
/*
@ -104,7 +103,6 @@ public class BlockListener implements Listener {
* on block/tool broken yet simple compatibility stuff
*/
if (!info.hasVanillaDrops()) {
// event.setDropItems(false); // May not work
event.setCancelled(true);
event.getBlock().setType(Material.AIR);
Bukkit.getPluginManager().callEvent(new PlayerItemDamageEvent(player, item, 1));

View File

@ -10,6 +10,8 @@ import java.util.Set;
import java.util.function.Function;
import java.util.logging.Level;
import javax.annotation.Nullable;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.block.Block;
@ -66,7 +68,14 @@ public class CustomBlockManager extends MMOManager {
map.put(regen.getBlock().generateKey(), regen);
}
public BlockInfo getInfo(Block block) {
/**
* Checks if the behaviour of a block was changed by a specific profession
* (different drop tables, block regen..)
*
* @param block Block to check
* @return The new block behaviour or null if no new behaviour
*/
public @Nullable BlockInfo getInfo(Block block) {
return map.getOrDefault(findBlockType(block).generateKey(), null);
}
@ -84,12 +93,11 @@ public class CustomBlockManager extends MMOManager {
* Used when a block is being broken and MMOCore needs to regen it after X
* seconds. Also places the temporary block at the block location
*
* @param info
* Block info
* @param scheduleRegen
* If block regeneration should be scheduled or not. If the block
* broken is a temporary block and is part of a "block chain", no
* regen should be scheduled as there is already one
* @param info Block info
* @param scheduleRegen If block regeneration should be scheduled or not. If
* the block broken is a temporary block and is part of
* a "block chain", no regen should be scheduled as
* there is already one
*/
public void initialize(RegeneratingBlock info, boolean scheduleRegen) {
if (scheduleRegen) {
@ -105,12 +113,10 @@ public class CustomBlockManager extends MMOManager {
* Called when a block regens, either due to regen timer or because the
* server shuts down.
*
* @param info
* Block which must be regened
* @param shutdown
* Must be set to true if the server is shutting down. When the
* server shuts down, it iterates through active blocks. This
* prevents any issue when editing lists being iterated
* @param info Block which must be regened
* @param shutdown Must be set to true if the server is shutting down. When
* the server shuts down, it iterates through active blocks.
* This prevents any issue when editing lists being iterated
*/
private void regen(RegeneratingBlock info, boolean shutdown) {
Location infoLocation = info.getLocation();
@ -133,10 +139,9 @@ public class CustomBlockManager extends MMOManager {
}
/**
* @param block
* Potentially vanilla block being broken by a player
* @return Returns if the block being broken is a temporary block. If it is,
* players should not be able to break it
* @param block Potentially vanilla block being broken by a player
* @return Returns if the block being broken is a temporary block. If
* it is, players should not be able to break it
*/
public boolean isTemporaryBlock(Block block) {
Location loc = block.getLocation();