Some cleanup to BonusDrops & reducing expensive state calls

This commit is contained in:
nossr50 2019-05-04 00:48:35 -07:00
parent a919c45b09
commit 3e61dc6c5f
3 changed files with 139 additions and 93 deletions

View File

@ -0,0 +1,14 @@
package com.gmail.nossr50.datatypes.meta;
import com.gmail.nossr50.mcMMO;
import org.bukkit.metadata.FixedMetadataValue;
/**
* Stores how many bonus drops a block should give
*/
public class BonusDropMeta extends FixedMetadataValue {
public BonusDropMeta(int value, mcMMO plugin) {
super(plugin, value);
}
}

View File

@ -3,6 +3,7 @@ package com.gmail.nossr50.listeners;
import com.gmail.nossr50.config.MainConfig;
import com.gmail.nossr50.config.WorldBlacklist;
import com.gmail.nossr50.config.experience.ExperienceConfig;
import com.gmail.nossr50.datatypes.meta.BonusDropMeta;
import com.gmail.nossr50.datatypes.player.McMMOPlayer;
import com.gmail.nossr50.datatypes.skills.PrimarySkillType;
import com.gmail.nossr50.datatypes.skills.SuperAbilityType;
@ -40,6 +41,7 @@ import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.block.*;
import org.bukkit.inventory.ItemStack;
import org.bukkit.metadata.FixedMetadataValue;
import org.bukkit.metadata.MetadataValue;
import java.util.List;
@ -63,93 +65,20 @@ public class BlockListener implements Listener {
continue;
//TODO: Should just store the amount of drops in the metadata itself and use a loop
if (event.getBlock().getState().getMetadata(mcMMO.BONUS_DROPS_METAKEY).size() > 0) {
event.getBlock().getState().getWorld().dropItemNaturally(event.getBlockState().getLocation(), is);
event.getBlock().getState().removeMetadata(mcMMO.BONUS_DROPS_METAKEY, plugin);
} else if (event.getBlock().getState().getMetadata(mcMMO.tripleDrops).size() > 0) {
event.getBlock().getState().getWorld().dropItemNaturally(event.getBlockState().getLocation(), is);
event.getBlock().getState().getWorld().dropItemNaturally(event.getBlockState().getLocation(), is);
event.getBlock().getState().removeMetadata(mcMMO.tripleDrops, plugin);
}
}
}
if (event.getBlock().getMetadata(mcMMO.BONUS_DROPS_METAKEY).size() > 0) {
BonusDropMeta bonusDropMeta = (BonusDropMeta) event.getBlock().getMetadata(mcMMO.BONUS_DROPS_METAKEY).get(0);
int bonusCount = bonusDropMeta.asInt();
/*@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
public void onBlockDropItemEvent(BlockDropItemEvent event)
{
for(Item item : event.getItems())
{
ItemStack is = new ItemStack(item.getItemStack());
if(event.getBlock().getMetadata(mcMMO.BONUS_DROPS_METAKEY).size() > 0)
{
List<MetadataValue> metadataValue = event.getBlock().getMetadata(mcMMO.BONUS_DROPS_METAKEY);
BonusDrops bonusDrops = (BonusDrops) metadataValue.get(0);
Collection<ItemStack> potentialDrops = (Collection<ItemStack>) bonusDrops.value();
if(potentialDrops.contains(is))
for (int i = 0; i < bonusCount; i++)
{
event.getBlock().getState().getWorld().dropItemNaturally(event.getBlockState().getLocation(), is);
event.getBlock().getWorld().dropItemNaturally(event.getBlockState().getLocation(), is);
}
event.getBlock().removeMetadata(mcMMO.BONUS_DROPS_METAKEY, plugin);
} else {
if(event.getBlock().getMetadata(mcMMO.tripleDrops).size() > 0) {
List<MetadataValue> metadataValue = event.getBlock().getMetadata(mcMMO.tripleDrops);
BonusDrops bonusDrops = (BonusDrops) metadataValue.get(0);
Collection<ItemStack> potentialDrops = (Collection<ItemStack>) bonusDrops.value();
if (potentialDrops.contains(is)) {
event.getBlock().getState().getWorld().dropItemNaturally(event.getBlockState().getLocation(), is);
event.getBlock().getState().getWorld().dropItemNaturally(event.getBlockState().getLocation(), is);
}
event.getBlock().removeMetadata(mcMMO.tripleDrops, plugin);
}
}
}
}
/*@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
public void onBlockDropItemEvent(BlockDropItemEvent event)
{
for(Item item : event.getItems())
{
ItemStack is = new ItemStack(item.getItemStack());
if(event.getBlock().getMetadata(mcMMO.BONUS_DROPS_METAKEY).size() > 0)
{
List<MetadataValue> metadataValue = event.getBlock().getMetadata(mcMMO.BONUS_DROPS_METAKEY);
BonusDrops bonusDrops = (BonusDrops) metadataValue.get(0);
Collection<ItemStack> potentialDrops = (Collection<ItemStack>) bonusDrops.value();
if(potentialDrops.contains(is))
{
event.getBlock().getState().getWorld().dropItemNaturally(event.getBlockState().getLocation(), is);
}
event.getBlock().removeMetadata(mcMMO.BONUS_DROPS_METAKEY, plugin);
} else {
if(event.getBlock().getMetadata(mcMMO.tripleDrops).size() > 0) {
List<MetadataValue> metadataValue = event.getBlock().getMetadata(mcMMO.tripleDrops);
BonusDrops bonusDrops = (BonusDrops) metadataValue.get(0);
Collection<ItemStack> potentialDrops = (Collection<ItemStack>) bonusDrops.value();
if (potentialDrops.contains(is)) {
event.getBlock().getState().getWorld().dropItemNaturally(event.getBlockState().getLocation(), is);
event.getBlock().getState().getWorld().dropItemNaturally(event.getBlockState().getLocation(), is);
}
event.getBlock().removeMetadata(mcMMO.tripleDrops, plugin);
}
}
}
}*/
/**
* Monitor BlockPistonExtend events.
*
@ -208,7 +137,7 @@ public class BlockListener implements Listener {
if (WorldBlacklist.isWorldBlacklisted(event.getBlock().getWorld()))
return;
if (BlockUtils.shouldBeWatched(event.getBlock().getState())) {
if (BlockUtils.shouldBeWatched(event.getBlock())) {
mcMMO.getPlaceStore().setTrue(event.getBlock());
}
}
@ -230,7 +159,7 @@ public class BlockListener implements Listener {
return;
}
BlockState blockState = event.getBlock().getState();
BlockState blockState = event.getBlock();
/* Check if the blocks placed should be monitored so they do not give out XP in the future */
if (BlockUtils.shouldBeWatched(blockState)) {
@ -289,7 +218,7 @@ public class BlockListener implements Listener {
if (WorldBlacklist.isWorldBlacklisted(event.getBlock().getWorld()))
return;
BlockState blockState = event.getBlock().getState();
BlockState blockState = event.getBlock();
if (!BlockUtils.shouldBeWatched(blockState)) {
return;
@ -319,7 +248,7 @@ public class BlockListener implements Listener {
return;
}
BlockState blockState = event.getBlock().getState();
BlockState blockState = event.getBlock();
Location location = blockState.getLocation();
if (!BlockUtils.shouldBeWatched(blockState)) {
@ -425,7 +354,7 @@ public class BlockListener implements Listener {
return;
}
BlockState blockState = event.getBlock().getState();
BlockState blockState = event.getBlock();
ItemStack heldItem = player.getInventory().getItemInMainHand();
if (Herbalism.isRecentlyRegrown(blockState)) {
@ -467,7 +396,7 @@ public class BlockListener implements Listener {
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onBlockDamage(BlockDamageEvent event) {
Player player = event.getPlayer();
BlockState blockState = event.getBlock().getState();
BlockState blockState = event.getBlock();
/* WORLD BLACKLIST CHECK */
if (WorldBlacklist.isWorldBlacklisted(event.getBlock().getWorld()))
@ -607,7 +536,7 @@ public class BlockListener implements Listener {
}
BlockState blockState = event.getBlock().getState();
BlockState blockState = event.getBlock();
ItemStack heldItem = player.getInventory().getItemInMainHand();

View File

@ -1,5 +1,6 @@
package com.gmail.nossr50.util;
import com.gmail.nossr50.datatypes.meta.BonusDropMeta;
import com.gmail.nossr50.datatypes.skills.SubSkillType;
import com.gmail.nossr50.mcMMO;
import com.gmail.nossr50.skills.repair.Repair;
@ -7,6 +8,7 @@ import com.gmail.nossr50.skills.salvage.Salvage;
import com.gmail.nossr50.util.random.RandomChanceSkill;
import com.gmail.nossr50.util.random.RandomChanceUtil;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.block.BlockState;
import org.bukkit.block.data.Ageable;
import org.bukkit.block.data.BlockData;
@ -27,9 +29,9 @@ public final class BlockUtils {
*/
public static void markDropsAsBonus(BlockState blockState, boolean triple) {
if (triple)
blockState.setMetadata(mcMMO.tripleDrops, mcMMO.metadataValue);
blockState.setMetadata(mcMMO.BONUS_DROPS_METAKEY, new BonusDropMeta(2, mcMMO.p));
else
blockState.setMetadata(mcMMO.BONUS_DROPS_METAKEY, mcMMO.metadataValue);
blockState.setMetadata(mcMMO.BONUS_DROPS_METAKEY, new BonusDropMeta(1, mcMMO.p));
}
/**
@ -46,6 +48,16 @@ public final class BlockUtils {
return false;
}
/**
* Checks to see if a given block awards XP.
*
* @param block The {@link Block} of the block to check
* @return true if the block awards XP, false otherwise
*/
public static boolean shouldBeWatched(Block block) {
return affectedByGigaDrillBreaker(block.getType()) || affectedByGreenTerra(block.getType()) || affectedBySuperBreaker(block.getType()) || isLog(block.getType());
}
/**
* Checks to see if a given block awards XP.
*
@ -56,6 +68,16 @@ public final class BlockUtils {
return affectedByGigaDrillBreaker(blockState) || affectedByGreenTerra(blockState) || affectedBySuperBreaker(blockState) || isLog(blockState);
}
/**
* Checks to see if a given block awards XP.
*
* @param material The {@link Material} of the block to check
* @return true if the block awards XP, false otherwise
*/
public static boolean shouldBeWatched(Material material) {
return affectedByGigaDrillBreaker(material) || affectedByGreenTerra(material) || affectedBySuperBreaker(material) || isLog(material);
}
/**
* Check if a given block should allow for the activation of abilities
*
@ -109,6 +131,16 @@ public final class BlockUtils {
return mcMMO.getConfigManager().getExperienceMapManager().hasHerbalismXp(blockState.getType());
}
/**
* Determine if a given block should be affected by Green Terra
*
* @param material The {@link Material} of the block to check
* @return true if the block should affected by Green Terra, false otherwise
*/
public static boolean affectedByGreenTerra(Material material) {
return mcMMO.getConfigManager().getExperienceMapManager().hasHerbalismXp(material);
}
/**
* Determine if a given block should be affected by Super Breaker
*
@ -123,8 +155,27 @@ public final class BlockUtils {
return isMineable(blockState);
}
public static boolean isMineable(BlockState blockState) {
switch (blockState.getType()) {
/**
* Determine if a given block should be affected by Super Breaker
*
* @param material The {@link Material} of the block to check
* @return true if the block should affected by Super Breaker, false
* otherwise
*/
public static Boolean affectedBySuperBreaker(Material material) {
if (mcMMO.getConfigManager().getExperienceMapManager().hasMiningXp(material))
return true;
return isMineable(material);
}
/**
* Whether or not a block is gathered via Pickaxes
* @param material target blocks material
* @return
*/
public static boolean isMineable(Material material) {
switch (material) {
case COAL_ORE:
case DIAMOND_ORE:
case EMERALD_ORE:
@ -150,6 +201,23 @@ public final class BlockUtils {
}
}
public static boolean isMineable(BlockState blockState) {
return isMineable(blockState.getType());
}
/**
* Determine if a given block should be affected by Giga Drill Breaker
*
* @param material The {@link Material} of the block to check
* @return true if the block should affected by Giga Drill Breaker, false
* otherwise
*/
public static boolean affectedByGigaDrillBreaker(Material material) {
if (mcMMO.getConfigManager().getExperienceMapManager().hasExcavationXp(material))
return true;
return isDiggable(material);
}
/**
* Determine if a given block should be affected by Giga Drill Breaker
@ -171,8 +239,19 @@ public final class BlockUtils {
* @param blockState target blockstate
* @return true if a shovel is typically used for digging this block
*/
@Deprecated
public static boolean isDiggable(BlockState blockState) {
switch (blockState.getType()) {
return isDiggable(blockState.getType());
}
/**
* Returns true if a shovel is used for digging this block
*
* @param material target blocks material
* @return true if a shovel is typically used for digging this block
*/
public static boolean isDiggable(Material material) {
switch (material) {
case CLAY:
case FARMLAND:
case GRASS_BLOCK:
@ -207,14 +286,28 @@ public final class BlockUtils {
//return mcMMO.getModManager().isCustomLog(blockState);
}
/**
* Check if a given block is a log
*
* @param material The {@link Material} of the block to check
* @return true if the block is a log, false otherwise
*/
public static boolean isLog(Material material) {
if (mcMMO.getConfigManager().getExperienceMapManager().hasWoodcuttingXp(material))
return true;
return isLoggingRelated(material);
//return mcMMO.getModManager().isCustomLog(blockState);
}
/**
* Determines if this particular block is typically gathered using an Axe
*
* @param blockState target blockstate
* @param material target material
* @return true if the block is gathered via axe
*/
public static boolean isLoggingRelated(BlockState blockState) {
switch (blockState.getType()) {
public static boolean isLoggingRelated(Material material) {
switch (material) {
case ACACIA_LOG:
case BIRCH_LOG:
case DARK_OAK_LOG:
@ -245,6 +338,16 @@ public final class BlockUtils {
}
}
/**
* Determines if this particular block is typically gathered using an Axe
*
* @param blockState target blockstate
* @return true if the block is gathered via axe
*/
public static boolean isLoggingRelated(BlockState blockState) {
return isLoggingRelated(blockState.getType());
}
/**
* Check if a given block is a leaf
*