Skip to content

Commit

Permalink
Merge pull request #133 from EndlessCodeGroup/rewrites
Browse files Browse the repository at this point in the history
I almost read your code (t/o)
  • Loading branch information
Dereku authored Apr 15, 2018
2 parents 0400a05 + 3a99fe4 commit ed28a2d
Show file tree
Hide file tree
Showing 6 changed files with 137 additions and 105 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@
package ru.endlesscode.rpginventory.event.listener;

import org.bukkit.Location;
import org.bukkit.block.BlockFace;
import org.bukkit.block.Block;
import org.bukkit.entity.Entity;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
Expand All @@ -32,7 +34,7 @@
import org.bukkit.event.inventory.InventoryType;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.inventory.ItemStack;
import org.jetbrains.annotations.*;
import org.jetbrains.annotations.NotNull;
import ru.endlesscode.rpginventory.RPGInventory;
import ru.endlesscode.rpginventory.api.InventoryAPI;
import ru.endlesscode.rpginventory.inventory.ActionType;
Expand All @@ -44,6 +46,8 @@
import ru.endlesscode.rpginventory.utils.ItemUtils;
import ru.endlesscode.rpginventory.utils.PlayerUtils;

import java.util.Collection;

/**
* Created by OsipXD on 08.04.2016
* It is part of the RpgInventory.
Expand Down Expand Up @@ -144,48 +148,75 @@ public void onNormalEquip(@NotNull InventoryClickEvent event) {
}

@EventHandler
public void onDispenseEquip(@NotNull BlockDispenseEvent event) {
public void onDispenseEquip(BlockDispenseEvent event) {
ArmorType type = ArmorType.matchType(event.getItem());
Location blockLoc = event.getBlock().getLocation();
Collection<Entity> nearbyEntities = blockLoc.getWorld().getNearbyEntities(blockLoc, 3D, 1.2D, 3D);
for (Entity entity : nearbyEntities) {
if (entity.getType() != EntityType.PLAYER) {
continue;
}

for (Player player : blockLoc.getWorld().getPlayers()) {
Location playerLoc = player.getLocation();

if (blockLoc.getBlockY() - playerLoc.getBlockY() >= -1
&& blockLoc.getBlockY() - playerLoc.getBlockY() <= 1) {

if (type == ArmorType.HELMET && player.getInventory().getHelmet() == null
|| type == ArmorType.CHESTPLATE && player.getInventory().getChestplate() == null
|| type == ArmorType.LEGGINGS && player.getInventory().getLeggings() == null
|| type == ArmorType.BOOTS && player.getInventory().getBoots() == null) {

org.bukkit.block.Dispenser dispenser = (org.bukkit.block.Dispenser) event.getBlock().getState();
org.bukkit.material.Dispenser dis = (org.bukkit.material.Dispenser) dispenser.getData();
BlockFace directionFacing = dis.getFacing();

// Someone told me not to do big if checks because it's hard to read, look at me doing it -_-
if (directionFacing == BlockFace.EAST && playerLoc.getBlockX() != blockLoc.getBlockX()
&& playerLoc.getX() <= blockLoc.getX() + 2.3 && playerLoc.getX() >= blockLoc.getX()
|| directionFacing == BlockFace.WEST && playerLoc.getX() >= blockLoc.getX() - 1.3
&& playerLoc.getX() <= blockLoc.getX()
|| directionFacing == BlockFace.SOUTH
&& playerLoc.getBlockZ() != blockLoc.getBlockZ()
&& playerLoc.getZ() <= blockLoc.getZ() + 2.3 && playerLoc.getZ() >= blockLoc.getZ()
|| directionFacing == BlockFace.NORTH && playerLoc.getZ() >= blockLoc.getZ() - 1.3
&& playerLoc.getZ() <= blockLoc.getZ()) {

if (!InventoryManager.playerIsLoaded(player)) {
return;
}

Slot armorSlot = SlotManager.instance().getSlot(type.name());
event.setCancelled(armorSlot != null
&& !InventoryManager.validateArmor(
player, InventoryAction.PLACE_ONE, armorSlot, event.getItem()));
return;
}
}
Player player = (Player) entity;
if (!this.isPlayerInRightPosition(event.getBlock(), player)) {
continue;
}
if (this.hasInventoryArmorByType(type, player)) {
continue;
}
if (InventoryManager.playerIsLoaded(player)) {
Slot armorSlot = SlotManager.instance().getSlot(type.name());
event.setCancelled(armorSlot != null
&& !InventoryManager.validateArmor(player, InventoryAction.PLACE_ONE, armorSlot, event.getItem())
);
return;
}
}
}

//Read helpers \:D/
private boolean hasInventoryArmorByType(ArmorType type, Player player) {
switch (type) {
case HELMET:
return player.getInventory().getHelmet() != null;
case CHESTPLATE:
return player.getInventory().getChestplate() != null;
case LEGGINGS:
return player.getInventory().getLeggings() != null;
case BOOTS:
return player.getInventory().getBoots() != null;
case UNKNOWN:
default:
return true; //Why no?
}
}

private boolean isPlayerInRightPosition(Block block, Player player) {
final Location blockLoc = block.getLocation();
final Location playerLoc = player.getLocation();
org.bukkit.block.Dispenser dispenser = (org.bukkit.block.Dispenser) block.getState();
org.bukkit.material.Dispenser dispenserData = (org.bukkit.material.Dispenser) dispenser.getData();
/*
From old 'if' statement
// Someone told me not to do big if checks because it's hard to read, look at me doing it -_-
directionFacing == BlockFace.EAST && playerLoc.getBlockX() != blockLoc.getBlockX() && playerLoc.getX() <= blockLoc.getX() + 2.3 && playerLoc.getX() >= blockLoc.getX()
|| directionFacing == BlockFace.WEST && playerLoc.getX() >= blockLoc.getX() - 1.3 && playerLoc.getX() <= blockLoc.getX()
|| directionFacing == BlockFace.SOUTH && playerLoc.getBlockZ() != blockLoc.getBlockZ() && playerLoc.getZ() <= blockLoc.getZ() + 2.3 && playerLoc.getZ() >= lockLoc.getZ()
|| directionFacing == BlockFace.NORTH && playerLoc.getZ() >= blockLoc.getZ() - 1.3 && playerLoc.getZ() <= blockLoc.getZ()+
*/
switch (dispenserData.getFacing()) {
case EAST:
return playerLoc.getBlockX() != blockLoc.getBlockX() && playerLoc.getX() <= blockLoc.getX() + 2.3 && playerLoc.getX() >= blockLoc.getX();
case WEST:
return playerLoc.getX() >= blockLoc.getX() - 1.3 && playerLoc.getX() <= blockLoc.getX();
case SOUTH:
return playerLoc.getBlockZ() != blockLoc.getBlockZ() && playerLoc.getZ() <= blockLoc.getZ() + 2.3 && playerLoc.getZ() >= blockLoc.getZ();
case NORTH:
return playerLoc.getZ() >= blockLoc.getZ() - 1.3 && playerLoc.getZ() <= blockLoc.getZ();
default:
return false;

}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import org.bukkit.GameMode;
import org.bukkit.OfflinePlayer;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
Expand All @@ -44,8 +45,8 @@
*/
public class InventoryLocker {

private static ItemStack lockedSlot = null;
private static ItemStack buyableSlot = null;
private static ItemStack LOCKED_SLOT = null;
private static ItemStack BUYABLE_SLOT = null;

private InventoryLocker() {
}
Expand All @@ -57,24 +58,23 @@ public static boolean init(@NotNull RPGInventory instance) {

try {
// Setup locked slot
lockedSlot = ItemUtils.getTexturedItem(Config.getConfig().getString("slots.locked"));
ItemMeta meta = lockedSlot.getItemMeta();
InventoryLocker.LOCKED_SLOT = ItemUtils.getTexturedItem(Config.getConfig().getString("slots.locked"));
ItemMeta meta = InventoryLocker.LOCKED_SLOT.getItemMeta();
meta.setDisplayName(RPGInventory.getLanguage().getMessage("locked.name"));
meta.setLore(Collections.singletonList(RPGInventory.getLanguage().getMessage("locked.lore")));

lockedSlot.setItemMeta(meta);
lockedSlot = addId(lockedSlot);
InventoryLocker.LOCKED_SLOT.setItemMeta(meta);
InventoryLocker.LOCKED_SLOT = addId(InventoryLocker.LOCKED_SLOT);

// Setup buyable slot
buyableSlot = ItemUtils.getTexturedItem(Config.getConfig().getString("slots.buyable"));
meta = buyableSlot.getItemMeta();
InventoryLocker.BUYABLE_SLOT = ItemUtils.getTexturedItem(Config.getConfig().getString("slots.buyable"));
meta = InventoryLocker.BUYABLE_SLOT.getItemMeta();
meta.setDisplayName(RPGInventory.getLanguage().getMessage("buyable.name"));
meta.setLore(Collections.singletonList(RPGInventory.getLanguage().getMessage("buyable.lore")));

buyableSlot.setItemMeta(meta);
buyableSlot = addId(buyableSlot);
InventoryLocker.BUYABLE_SLOT.setItemMeta(meta);
InventoryLocker.BUYABLE_SLOT = addId(InventoryLocker.BUYABLE_SLOT);
} catch (Exception e) {

e.printStackTrace();
return false;
}
Expand All @@ -88,14 +88,16 @@ private static boolean isEnabled() {
}

public static boolean buySlot(@NotNull Player player, int line) {
if (RPGInventory.economyConnected() && RPGInventory.getEconomy().withdrawPlayer(player, Config.getConfig().getDouble("slots.money.cost.line" + line)).transactionSuccess()) {
if (RPGInventory.getLevelSystem() == PlayerUtils.LevelSystem.EXP && Config.getConfig().getBoolean("slots.level.spend")) {
player.setLevel(player.getLevel() - Config.getConfig().getInt("slots.level.required.line" + line));
if (!RPGInventory.economyConnected()) {
return false;
}
final FileConfiguration config = Config.getConfig();
if (RPGInventory.getEconomy().withdrawPlayer(player, config.getDouble("slots.money.cost.line" + line)).transactionSuccess()) {
if (RPGInventory.getLevelSystem() == PlayerUtils.LevelSystem.EXP && config.getBoolean("slots.level.spend")) {
player.setLevel(player.getLevel() - config.getInt("slots.level.required.line" + line));
}

return true;
}

return false;
}

Expand All @@ -106,17 +108,17 @@ public static int getLine(int slot) {

@NotNull
public static ItemStack getBuyableSlotForLine(int line) {
ItemStack slot = buyableSlot.clone();
ItemStack slot = InventoryLocker.BUYABLE_SLOT.clone();
ItemMeta im = slot.getItemMeta();
List<String> lore = im.getLore();
FileLanguage lang = RPGInventory.getLanguage();

if (Config.getConfig().getBoolean("slots.money.enabled")) {
lore.add(lang.getMessage("buyable.money", StringUtils.doubleToString(Config.getConfig().getDouble("slots.money.cost.line" + line))));
final FileConfiguration config = Config.getConfig();
if (config.getBoolean("slots.money.enabled")) {
lore.add(lang.getMessage("buyable.money", StringUtils.doubleToString(config.getDouble("slots.money.cost.line" + line))));
}

if (Config.getConfig().getBoolean("slots.level.enabled")) {
lore.add(lang.getMessage("buyable.level", Config.getConfig().getInt("slots.level.required.line" + line)));
if (config.getBoolean("slots.level.enabled")) {
lore.add(lang.getMessage("buyable.level", config.getInt("slots.level.required.line" + line)));
}
im.setLore(lore);
slot.setItemMeta(im);
Expand All @@ -130,11 +132,11 @@ private static ItemStack addId(ItemStack item) {
}

public static boolean isLockedSlot(@Nullable ItemStack item) {
return isEnabled() && !ItemUtils.isEmpty(item) && ItemUtils.hasTag(item, "locked");
return InventoryLocker.isEnabled() && !ItemUtils.isEmpty(item) && ItemUtils.hasTag(item, "locked");
}

public static boolean isBuyableSlot(ItemStack currentItem, int line) {
return getBuyableSlotForLine(line).equals(currentItem);
return InventoryLocker.getBuyableSlotForLine(line).equals(currentItem);
}

public static void lockSlots(@NotNull Player player) {
Expand All @@ -146,10 +148,10 @@ public static void lockSlots(@NotNull Player player, boolean force) {
return;
}

if (isEnabled()) {
if (InventoryLocker.isEnabled()) {
int maxSlot = getSlots(player) + 8;
for (int i = 35; i > maxSlot; i--) {
player.getInventory().setItem(i, lockedSlot);
player.getInventory().setItem(i, InventoryLocker.LOCKED_SLOT);
}

if (maxSlot < 35) {
Expand All @@ -162,10 +164,10 @@ public static void lockSlots(@NotNull Player player, boolean force) {
}

public static void unlockSlots(@NotNull Player player) {
if (isEnabled()) {
if (InventoryLocker.isEnabled()) {
for (int i = 8 + getSlots(player); i < 36; i++) {
ItemStack itemStack = player.getInventory().getItem(i);
if (isLockedSlot(itemStack)) {
if (InventoryLocker.isLockedSlot(itemStack)) {
player.getInventory().setItem(i, null);
}
}
Expand All @@ -176,16 +178,17 @@ public static void unlockSlots(@NotNull Player player) {
}

public static boolean canBuySlot(@NotNull Player player, int line) {
if (Config.getConfig().getBoolean("slots.money.enabled")) {
double cost = Config.getConfig().getDouble("slots.money.cost.line" + line);
final FileConfiguration config = Config.getConfig();
if (config.getBoolean("slots.money.enabled")) {
double cost = config.getDouble("slots.money.cost.line" + line);

if (!PlayerUtils.checkMoney(player, cost)) {
return false;
}
}

if (Config.getConfig().getBoolean("slots.level.enabled")) {
int requirement = Config.getConfig().getInt("slots.level.required.line" + line);
if (config.getBoolean("slots.level.enabled")) {
int requirement = config.getInt("slots.level.required.line" + line);

if (!PlayerUtils.checkLevel(player, requirement)) {
PlayerUtils.sendMessage(player, RPGInventory.getLanguage().getMessage("error.level", requirement));
Expand Down
Loading

0 comments on commit ed28a2d

Please sign in to comment.