mirror of
https://github.com/mcMMO-Dev/mcMMO.git
synced 2024-11-22 21:26:46 +01:00
More code duplication removal.
This commit is contained in:
parent
db637f0bc9
commit
3a78e1b487
@ -1,8 +1,6 @@
|
|||||||
package com.gmail.nossr50.listeners;
|
package com.gmail.nossr50.listeners;
|
||||||
|
|
||||||
import org.bukkit.block.Block;
|
import org.bukkit.block.Block;
|
||||||
import org.bukkit.block.BlockState;
|
|
||||||
import org.bukkit.block.Furnace;
|
|
||||||
import org.bukkit.entity.HumanEntity;
|
import org.bukkit.entity.HumanEntity;
|
||||||
import org.bukkit.entity.Player;
|
import org.bukkit.entity.Player;
|
||||||
import org.bukkit.event.EventHandler;
|
import org.bukkit.event.EventHandler;
|
||||||
@ -21,7 +19,6 @@ import org.bukkit.metadata.FixedMetadataValue;
|
|||||||
import com.gmail.nossr50.mcMMO;
|
import com.gmail.nossr50.mcMMO;
|
||||||
import com.gmail.nossr50.datatypes.skills.SkillType;
|
import com.gmail.nossr50.datatypes.skills.SkillType;
|
||||||
import com.gmail.nossr50.runnables.PlayerUpdateInventoryTask;
|
import com.gmail.nossr50.runnables.PlayerUpdateInventoryTask;
|
||||||
import com.gmail.nossr50.skills.smelting.SmeltingManager;
|
|
||||||
import com.gmail.nossr50.util.ItemUtils;
|
import com.gmail.nossr50.util.ItemUtils;
|
||||||
import com.gmail.nossr50.util.Misc;
|
import com.gmail.nossr50.util.Misc;
|
||||||
import com.gmail.nossr50.util.Permissions;
|
import com.gmail.nossr50.util.Permissions;
|
||||||
@ -68,15 +65,9 @@ public class InventoryListener implements Listener {
|
|||||||
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
|
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
|
||||||
public void onFurnaceBurnEvent(FurnaceBurnEvent event) {
|
public void onFurnaceBurnEvent(FurnaceBurnEvent event) {
|
||||||
Block furnaceBlock = event.getBlock();
|
Block furnaceBlock = event.getBlock();
|
||||||
BlockState furnaceState = furnaceBlock.getState();
|
ItemStack smelting = Misc.getSmeltingFromFurnace(furnaceBlock);
|
||||||
|
|
||||||
if (!(furnaceState instanceof Furnace)) {
|
if (!ItemUtils.isSmeltable(smelting)) {
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
ItemStack smelting = ((Furnace) furnaceState).getInventory().getSmelting();
|
|
||||||
|
|
||||||
if (smelting == null || !ItemUtils.isSmeltable(smelting)) {
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -92,15 +83,9 @@ public class InventoryListener implements Listener {
|
|||||||
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
|
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
|
||||||
public void onFurnaceSmeltEvent(FurnaceSmeltEvent event) {
|
public void onFurnaceSmeltEvent(FurnaceSmeltEvent event) {
|
||||||
Block furnaceBlock = event.getBlock();
|
Block furnaceBlock = event.getBlock();
|
||||||
BlockState furnaceState = furnaceBlock.getState();
|
ItemStack smelting = Misc.getSmeltingFromFurnace(furnaceBlock);
|
||||||
|
|
||||||
if (!(furnaceState instanceof Furnace)) {
|
if (!ItemUtils.isSmeltable(smelting)) {
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
ItemStack smelting = ((Furnace) furnaceState).getInventory().getSmelting();
|
|
||||||
|
|
||||||
if (smelting == null || !ItemUtils.isSmeltable(smelting)) {
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -116,29 +101,19 @@ public class InventoryListener implements Listener {
|
|||||||
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
|
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
|
||||||
public void onFurnaceExtractEvent(FurnaceExtractEvent event) {
|
public void onFurnaceExtractEvent(FurnaceExtractEvent event) {
|
||||||
Block furnaceBlock = event.getBlock();
|
Block furnaceBlock = event.getBlock();
|
||||||
BlockState furnaceState = furnaceBlock.getState();
|
ItemStack result = Misc.getResultFromFurnace(furnaceBlock);
|
||||||
|
|
||||||
if (!(furnaceState instanceof Furnace)) {
|
if (!ItemUtils.isSmelted(result)) {
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
ItemStack result = ((Furnace) furnaceState).getInventory().getResult();
|
|
||||||
|
|
||||||
if (result == null || !ItemUtils.isSmelted(result)) {
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
Player player = Misc.getPlayerFromFurnace(furnaceBlock);
|
Player player = Misc.getPlayerFromFurnace(furnaceBlock);
|
||||||
|
|
||||||
if (Misc.isNPCEntity(player)) {
|
if (Misc.isNPCEntity(player) || !Permissions.vanillaXpBoost(player, SkillType.SMELTING)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
SmeltingManager smeltingManager = UserManager.getPlayer(player).getSmeltingManager();
|
event.setExpToDrop(UserManager.getPlayer(player).getSmeltingManager().vanillaXPBoost(event.getExpToDrop()));
|
||||||
|
|
||||||
if (smeltingManager.canUseVanillaXpBoost()) {
|
|
||||||
event.setExpToDrop(smeltingManager.vanillaXPBoost(event.getExpToDrop()));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
|
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
|
||||||
|
@ -28,10 +28,6 @@ public class SmeltingManager extends SkillManager {
|
|||||||
return getSkillLevel() >= Smelting.fluxMiningUnlockLevel && BlockUtils.affectedByFluxMining(blockState) && Permissions.fluxMining(getPlayer()) && !mcMMO.getPlaceStore().isTrue(blockState);
|
return getSkillLevel() >= Smelting.fluxMiningUnlockLevel && BlockUtils.affectedByFluxMining(blockState) && Permissions.fluxMining(getPlayer()) && !mcMMO.getPlaceStore().isTrue(blockState);
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean canUseVanillaXpBoost() {
|
|
||||||
return getSkillLevel() >= Smelting.Tier.ONE.getLevel() && Permissions.vanillaXpBoost(getPlayer(), skill);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Process the Flux Mining ability.
|
* Process the Flux Mining ability.
|
||||||
*
|
*
|
||||||
@ -122,6 +118,6 @@ public class SmeltingManager extends SkillManager {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
return 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -461,6 +461,10 @@ public class ItemUtils {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static boolean isSmeltable(ItemStack item) {
|
public static boolean isSmeltable(ItemStack item) {
|
||||||
|
if (item == null) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
switch (item.getType()) {
|
switch (item.getType()) {
|
||||||
case COAL_ORE:
|
case COAL_ORE:
|
||||||
case DIAMOND_ORE:
|
case DIAMOND_ORE:
|
||||||
@ -479,6 +483,10 @@ public class ItemUtils {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static boolean isSmelted(ItemStack item) {
|
public static boolean isSmelted(ItemStack item) {
|
||||||
|
if (item == null) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
switch (item.getType()) {
|
switch (item.getType()) {
|
||||||
case COAL:
|
case COAL:
|
||||||
case DIAMOND:
|
case DIAMOND:
|
||||||
|
@ -8,6 +8,7 @@ import org.bukkit.Chunk;
|
|||||||
import org.bukkit.Location;
|
import org.bukkit.Location;
|
||||||
import org.bukkit.Material;
|
import org.bukkit.Material;
|
||||||
import org.bukkit.block.Block;
|
import org.bukkit.block.Block;
|
||||||
|
import org.bukkit.block.BlockState;
|
||||||
import org.bukkit.block.Furnace;
|
import org.bukkit.block.Furnace;
|
||||||
import org.bukkit.entity.Entity;
|
import org.bukkit.entity.Entity;
|
||||||
import org.bukkit.entity.HumanEntity;
|
import org.bukkit.entity.HumanEntity;
|
||||||
@ -230,7 +231,31 @@ public final class Misc {
|
|||||||
public static Player getPlayerFromFurnace(Block furnaceBlock) {
|
public static Player getPlayerFromFurnace(Block furnaceBlock) {
|
||||||
List<MetadataValue> metadata = furnaceBlock.getMetadata(mcMMO.furnaceMetadataKey);
|
List<MetadataValue> metadata = furnaceBlock.getMetadata(mcMMO.furnaceMetadataKey);
|
||||||
|
|
||||||
return metadata.isEmpty() ? null : mcMMO.p.getServer().getPlayerExact(metadata.get(0).asString());
|
if (metadata.isEmpty()) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
return mcMMO.p.getServer().getPlayerExact(metadata.get(0).asString());
|
||||||
|
}
|
||||||
|
|
||||||
|
public static ItemStack getSmeltingFromFurnace(Block furnaceBlock) {
|
||||||
|
BlockState furnaceState = furnaceBlock.getState();
|
||||||
|
|
||||||
|
if (!(furnaceState instanceof Furnace)) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
return ((Furnace) furnaceState).getInventory().getSmelting();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static ItemStack getResultFromFurnace(Block furnaceBlock) {
|
||||||
|
BlockState furnaceState = furnaceBlock.getState();
|
||||||
|
|
||||||
|
if (!(furnaceState instanceof Furnace)) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
return ((Furnace) furnaceState).getInventory().getResult();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Random getRandom() {
|
public static Random getRandom() {
|
||||||
|
Loading…
Reference in New Issue
Block a user