-
-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add 'CollectExperienceAtSource' option in config.yml
- Loading branch information
Showing
10 changed files
with
193 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
92 changes: 92 additions & 0 deletions
92
veinminer-bukkit/src/main/java/wtf/choco/veinminer/listener/BlockDropCollectionListener.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
veinminer-bukkit/src/main/java/wtf/choco/veinminer/listener/ExperienceTracker.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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))); | ||
} | ||
|
||
} |
53 changes: 0 additions & 53 deletions
53
veinminer-bukkit/src/main/java/wtf/choco/veinminer/listener/ItemCollectionListener.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters