Skip to content

Commit

Permalink
Add 'CollectExperienceAtSource' option in config.yml
Browse files Browse the repository at this point in the history
  • Loading branch information
2008Choco committed Feb 12, 2024
1 parent 9bdca05 commit bb3a2fc
Show file tree
Hide file tree
Showing 10 changed files with 193 additions and 55 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@
import wtf.choco.veinminer.economy.SimpleVaultEconomy;
import wtf.choco.veinminer.integration.PlaceholderExpansionVeinMiner;
import wtf.choco.veinminer.integration.WorldGuardIntegration;
import wtf.choco.veinminer.listener.BlockDropCollectionListener;
import wtf.choco.veinminer.listener.BreakBlockListener;
import wtf.choco.veinminer.listener.ItemCollectionListener;
import wtf.choco.veinminer.listener.McMMOIntegrationListener;
import wtf.choco.veinminer.listener.PlayerDataListener;
import wtf.choco.veinminer.manager.VeinMinerManager;
Expand Down Expand Up @@ -174,8 +174,8 @@ public void onEnable() {

// Register events
this.getLogger().info("Registering events");
manager.registerEvents(new BlockDropCollectionListener(this), this);
manager.registerEvents(new BreakBlockListener(this), this);
manager.registerEvents(new ItemCollectionListener(this), this);
manager.registerEvents(new PlayerDataListener(this), this);

Plugin mcMMOPlugin = manager.getPlugin("mcMMO");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,21 @@ public interface VeinMinerConfiguration extends VeinMiningConfiguration {
*/
public boolean isCollectItemsAtSource();

/**
* Get whether or not experience should be collected and dropped at the origin location.
* <p>
* If this value returns true, after a player completes a vein mine, all the experience that
* would have dropped as a result of all the blocks being broken will be dropped at the location
* where the original block was broken. If false, the experience orbs will drop individually at
* the block's broken position, as though the player broke each block separately.
* <p>
* If this value is not explicitly set, it will default to {@link #isCollectItemsAtSource()}.
*
* @return true if experience will be collected at the source, false if dropped at each block's
* location
*/
public boolean isCollectExperienceAtSource();

/**
* Get whether or not McMMO's experience system should be nerfed while vein mining.
* <p>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ final class ConfigKeys {
static final String KEY_DEFAULT_ACTIVATION_STRATEGY = "DefaultActivationStrategy";
static final String KEY_DEFAULT_VEIN_MINING_PATTERN = "DefaultVeinMiningPattern";
static final String KEY_COLLECT_ITEMS_AT_SOURCE = "CollectItemsAtSource";
static final String KEY_COLLECT_EXPERIENCE_AT_SOURCE = "CollectExperienceAtSource";
static final String KEY_NERF_MCMMO = "NerfMcMMO";
static final String KEY_DISABLED_GAME_MODES = "DisabledGameModes";
static final String KEY_HUNGER_HUNGER_MODIFIER = "Hunger.HungerModifier";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,11 @@ public boolean isCollectItemsAtSource() {
return plugin.getConfig().getBoolean(KEY_COLLECT_ITEMS_AT_SOURCE, true);
}

@Override
public boolean isCollectExperienceAtSource() {
return plugin.getConfig().getBoolean(KEY_COLLECT_EXPERIENCE_AT_SOURCE, isCollectItemsAtSource());
}

@Override
public boolean isNerfMcMMO() {
return plugin.getConfig().getBoolean(KEY_NERF_MCMMO, false);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package wtf.choco.veinminer.listener;

import java.util.List;

import org.bukkit.Location;
import org.bukkit.block.Block;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.block.BlockDropItemEvent;
import org.bukkit.event.block.BlockExpEvent;
import org.bukkit.metadata.MetadataValue;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import wtf.choco.veinminer.VeinMinerPlugin;
import wtf.choco.veinminer.util.VMConstants;

public final class BlockDropCollectionListener implements Listener {

private final VeinMinerPlugin plugin;

public BlockDropCollectionListener(@NotNull VeinMinerPlugin plugin) {
this.plugin = plugin;
}

@EventHandler(priority = EventPriority.HIGH, ignoreCancelled = true)
private void onDropVeinMinedItem(BlockDropItemEvent event) {
Block block = event.getBlock();
if (!block.hasMetadata(VMConstants.METADATA_KEY_TO_BE_VEINMINED)) {
return;
}

if (!plugin.getConfiguration().isCollectItemsAtSource()) {
return;
}

Location source = find(block, VMConstants.METADATA_KEY_VEINMINER_SOURCE, Location.class);
if (source == null) {
return;
}

Location sourceFinal = source.clone().add(0.5, 0.5, 0.5);
event.getItems().forEach(item -> item.teleport(sourceFinal));
}

@EventHandler(priority = EventPriority.HIGH, ignoreCancelled = true)
private void onDropVeinMinedExperience(BlockExpEvent event) {
int experience = event.getExpToDrop();
if (experience <= 0) {
return;
}

Block block = event.getBlock();
if (!block.hasMetadata(VMConstants.METADATA_KEY_TO_BE_VEINMINED)) {
return;
}

if (!plugin.getConfiguration().isCollectExperienceAtSource()) {
return;
}

Location source = find(block, VMConstants.METADATA_KEY_VEINMINER_SOURCE, Location.class);
if (source == null) {
return;
}

Block sourceBlock = source.getBlock();
ExperienceTracker experienceTracker = find(sourceBlock, VMConstants.METADATA_KEY_VEINMINER_EXPERIENCE, ExperienceTracker.class);
if (experienceTracker == null) {
return;
}

experienceTracker.pushExperience(experience);
event.setExpToDrop(0);
}

@Nullable
private <T> T find(@NotNull Block block, @NotNull String metadataKey, @NotNull Class<T> clazz) {
List<MetadataValue> values = block.getMetadata(metadataKey);

for (MetadataValue value : values) {
Object sourceObject = value.value();
if (clazz.isInstance(sourceObject)) {
return clazz.cast(sourceObject);
}
}

return null;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.FluidCollisionMode;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.block.BlockFace;
Expand Down Expand Up @@ -142,6 +143,12 @@ private void onBlockBreak(BlockBreakEvent event) {
block.setMetadata(VMConstants.METADATA_KEY_VEINMINER_SOURCE, new LazyMetadataValue(plugin, CacheStrategy.CACHE_ETERNALLY, origin::getLocation));
});

ExperienceTracker experienceTracker = null;
if (plugin.getConfiguration().isCollectExperienceAtSource()) {
experienceTracker = new ExperienceTracker();
origin.setMetadata(VMConstants.METADATA_KEY_VEINMINER_EXPERIENCE, new FixedMetadataValue(plugin, experienceTracker));
}

// Anticheat support
List<AntiCheatHook> hooks = plugin.getAnticheatHooks();
hooks.forEach(h -> h.exempt(player));
Expand Down Expand Up @@ -199,6 +206,13 @@ private void onBlockBreak(BlockBreakEvent event) {
block.removeMetadata(VMConstants.METADATA_KEY_VEINMINER_SOURCE, plugin);
});

// Handle experience dropping if necessary
if (experienceTracker != null && experienceTracker.hasExperience()) {
Location experienceLocation = origin.getLocation().add(0.5, 0.5, 0.5);
experienceTracker.spawnExperienceOrbsAt(experienceLocation);
origin.removeMetadata(VMConstants.METADATA_KEY_VEINMINER_EXPERIENCE, plugin);
}

// VEINMINER - DONE

// Unexempt from anticheats
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package wtf.choco.veinminer.listener;

import com.google.common.base.Preconditions;

import java.util.ArrayList;
import java.util.List;

import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.entity.ExperienceOrb;
import org.jetbrains.annotations.NotNull;

/**
* A simple class tracking experience values to later spawn them at a specific {@link Location}.
*/
public final class ExperienceTracker {

private List<Integer> experienceValues;

/**
* Construct a new {@link ExperienceTracker}.
*/
ExperienceTracker() { }

/**
* Push an experience value to this tracker.
*
* @param experience the experience value
*/
public void pushExperience(int experience) {
Preconditions.checkArgument(experience > 0, "experience must be > 0, was %s", experience);

if (experienceValues == null) {
this.experienceValues = new ArrayList<>();
}

this.experienceValues.add(experience);
}

/**
* Check whether or not the tracker has tracked any experience.
*
* @return true if there is at least one experience value, false if there are none
*/
public boolean hasExperience() {
return experienceValues != null;
}

/**
* Spawn a series of experience orbs at the given {@link Location} matching the amount and
* values pushed to this tracker.
*
* @param location the location at which to spawn the experience orbs
*/
public void spawnExperienceOrbsAt(@NotNull Location location) {
World world = location.getWorld();
Preconditions.checkArgument(world != null, "location.getWorld() must not be null");

this.experienceValues.forEach(experience -> world.spawn(location, ExperienceOrb.class, orb -> orb.setExperience(experience)));
}

}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public final class VMConstants {
// Metadata keys
public static final String METADATA_KEY_TO_BE_VEINMINED = "veinminer:to_be_veinmined";
public static final String METADATA_KEY_VEINMINER_SOURCE = "veinminer:source";
public static final String METADATA_KEY_VEINMINER_EXPERIENCE = "veinminer:experience";

public static final String METADATA_KEY_VEINMINING = "veinminer:vein_mining";
public static final String METADATA_KEY_VEIN_MINER_ACTIVE = "veinminer:vein_miner_active";
Expand Down
1 change: 1 addition & 0 deletions veinminer-bukkit/src/main/resources/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ PerformUpdateChecks: true
DefaultActivationStrategy: SNEAK
DefaultVeinMiningPattern: 'veinminer:default'
CollectItemsAtSource: true
CollectExperienceAtSource: true
NerfMcMMO: false

# "RepairFriendly", "MaxVeinSize", "Cost", and "DisabledWorlds" may be
Expand Down

0 comments on commit bb3a2fc

Please sign in to comment.