Skip to content

Commit

Permalink
Fix farming and change boss bar
Browse files Browse the repository at this point in the history
  • Loading branch information
Archy-X committed Sep 11, 2023
1 parent c09e768 commit 524d2c6
Show file tree
Hide file tree
Showing 12 changed files with 235 additions and 101 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public boolean isEnabled() {

@Override
public Ability getXpMultiplierAbility() {
return xpMultiplierAbility;
return xpMultiplierAbility.isEnabled() ? xpMultiplierAbility : null;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ public interface BlockXpSource extends XpSource {
*/
double getStateMultiplier(String stateKey, Object stateValue);

boolean hasStateMultiplier();

/**
* Gets whether the source requires a support block.
* If this is true, the source will only give xp if the block below it is a valid support block.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ private void checkSupportBelow(Block block) {
@Override
public void run() {
// Remove if block was destroyed
if (!blockLeveler.matchesSource(block, source, BlockXpSource.BlockTriggers.BREAK)) {
if (blockLeveler.isDifferentSource(block, source, BlockXpSource.BlockTriggers.BREAK)) {
regionManager.removePlacedBlock(above);
}
}
Expand All @@ -177,7 +177,7 @@ private void checkSupportSide(Block block) {
@Override
public void run() {
// Remove if block was destroyed
if (!blockLeveler.matchesSource(block, source, BlockXpSource.BlockTriggers.BREAK)) {
if (blockLeveler.isDifferentSource(block, source, BlockXpSource.BlockTriggers.BREAK)) {
regionManager.removePlacedBlock(block);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,11 @@

public class BlockLeveler extends SourceLeveler {

private final BlockLevelerHelper helper;

public BlockLeveler(AuraSkills plugin) {
super(plugin, SourceType.BLOCK);
this.helper = new BlockLevelerHelper(plugin);
}

@EventHandler
Expand All @@ -50,7 +53,10 @@ public void onBreak(BlockBreakEvent event) {

if (failsChecks(event, player, event.getBlock().getLocation(), skill)) return;

plugin.getLevelManager().addXp(user, skill, source.getXp());
double multiplier = helper.getBlocksBroken(event.getBlock(), source);
multiplier *= helper.getStateMultiplier(event.getBlock(), source);

plugin.getLevelManager().addXp(user, skill, source.getXp() * multiplier);
}

@EventHandler
Expand All @@ -72,7 +78,10 @@ public void onInteract(PlayerInteractEvent event) {

if (failsChecks(event, player, block.getLocation(), skill)) return;

plugin.getLevelManager().addXp(user, skill, source.getXp());
double multiplier = helper.getBlocksBroken(block, source);
multiplier *= helper.getStateMultiplier(block, source);

plugin.getLevelManager().addXp(user, skill, source.getXp() * multiplier);
applyAbilities(skill, player, user, block, source);
}

Expand All @@ -96,12 +105,12 @@ private void applyAbilities(Skill skill, Player player, User user, Block block,
}
}

public boolean matchesSource(Block block, BlockXpSource source, BlockXpSource.BlockTriggers trigger) {
public boolean isDifferentSource(Block block, BlockXpSource source, BlockXpSource.BlockTriggers trigger) {
var sourcePair = getSource(block, trigger);
if (sourcePair == null) {
return false;
return true;
}
return sourcePair.first().equals(source);
return !sourcePair.first().equals(source);
}

@Nullable
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package dev.aurelium.auraskills.bukkit.source;

import dev.aurelium.auraskills.api.source.type.BlockXpSource;
import dev.aurelium.auraskills.bukkit.AuraSkills;
import dev.aurelium.auraskills.common.config.Option;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.block.BlockFace;
import org.bukkit.block.BlockState;
import org.bukkit.block.data.Ageable;
import org.bukkit.block.data.type.SeaPickle;

public class BlockLevelerHelper {

private final AuraSkills plugin;

public BlockLevelerHelper(AuraSkills plugin) {
this.plugin = plugin;
}

/**
* Gets the total number of blocks broken when breaking
* multi-block plants, including the directly broken one.
*
* @param block The directly broken block
* @param source The XP source
* @return Number of blocks broken
*/
public int getBlocksBroken(Block block, BlockXpSource source) {
Material mat = block.getType();
if (mat == Material.SUGAR_CANE) {
int num = 1;
// Check the two blocks above
BlockState above = block.getRelative(BlockFace.UP).getState();
if (above.getType() == Material.SUGAR_CANE) {
if (!plugin.getRegionManager().isPlacedBlock(above.getBlock()) || !checkReplace()) {
num++;
}
BlockState aboveAbove = block.getRelative(BlockFace.UP).getRelative(BlockFace.UP).getState();
if (aboveAbove.getType() == Material.SUGAR_CANE) {
if (!plugin.getRegionManager().isPlacedBlock(aboveAbove.getBlock()) || !checkReplace()) {
num++;
}
}
}
return num;
} else if (mat == Material.BAMBOO || mat == Material.CACTUS || mat == Material.KELP_PLANT) {
int num = 1;
if (checkReplace() && plugin.getRegionManager().isPlacedBlock(block)) {
Block above = block.getRelative(BlockFace.UP);
if (!sourceMatches(block, source) || plugin.getRegionManager().isPlacedBlock(above)) {
return 0;
}
num = 0;
}
num += getSameBlocksAbove(block.getRelative(BlockFace.UP), source, 0);
return num;
}
return 1;
}

public double getStateMultiplier(Block block, BlockXpSource source) {
if (!source.hasStateMultiplier()) return 1.0;

if (block.getBlockData() instanceof Ageable ageable) {
return source.getStateMultiplier("age", ageable.getAge());
} else if (block.getBlockData() instanceof SeaPickle seaPickle) {
return source.getStateMultiplier("pickles", seaPickle.getPickles());
}
return source.getStateMultiplier("placeholder", 1.0);
}


private int getSameBlocksAbove(Block block, BlockXpSource source, int num) {
if (sourceMatches(block, source)) {
if (checkReplace() && plugin.getRegionManager().isPlacedBlock(block)) {
return num;
}
num++;
return getSameBlocksAbove(block.getRelative(BlockFace.UP), source, num);
}
return num;
}

private boolean sourceMatches(Block block, BlockXpSource source) {
for (String blockName : source.getBlocks()) {
if (block.getType().name().equalsIgnoreCase(blockName)) {
return true;
}
}
return false;
}

private boolean checkReplace() {
return plugin.configBoolean(Option.CHECK_BLOCK_REPLACE);
}

}
Loading

0 comments on commit 524d2c6

Please sign in to comment.