Skip to content

Commit

Permalink
2.1.6-PRE
Browse files Browse the repository at this point in the history
This update contains mainly bugfixes and some improvements.

### Improved:
* Work of pet cooldowns
* Performance when pets follow you

### Fixed:
* Many possible NPEs (such as #124)
* Error on EntityDamageByEntityEvent (#130)
* Pet duplication after teleportaton (#110)
* Copy the horse's brand bug (#120)
  • Loading branch information
osipxd authored Apr 26, 2018
2 parents c0a62cf + c13d909 commit a7a35e0
Show file tree
Hide file tree
Showing 12 changed files with 274 additions and 79 deletions.
10 changes: 7 additions & 3 deletions src/main/java/ru/endlesscode/rpginventory/api/PetAPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,21 +35,25 @@
@SuppressWarnings({"unused", "WeakerAccess"})
public class PetAPI {
/**
* Get pet spawn item from RPGInventory of specific player
* Get pet spawn item from RPGInventory of specific player.
*
* @param player - not null player
* @return ItemStack if player have pet spawn item, null - otherwise
*/
@Nullable
public static ItemStack getPetItem(Player player) {
if (!InventoryManager.playerIsLoaded(player) || !PetManager.isEnabled()) {
return null;
}

PlayerWrapper playerWrapper = InventoryManager.get(player);
ItemStack petItem = PetManager.isEnabled() && playerWrapper != null ? playerWrapper.getInventory().getItem(PetManager.getPetSlotId()) : null;
ItemStack petItem = playerWrapper.getInventory().getItem(PetManager.getPetSlotId());

return ItemUtils.isEmpty(petItem) ? null : petItem;
}

/**
* Get Pet of specific player
* Get Pet of specific player.
*
* @param player - not null player
* @return Pet if player have pet, null - otherwise
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public void onPacketSending(@NotNull PacketEvent event) {
Player player = event.getPlayer();
//noinspection ConstantConditions
if (event.isCancelled() || !InventoryManager.playerIsLoaded(player)
|| isNotNeededHere(player)) {
|| isExtensionsNotNeededHere(player)) {
return;
}

Expand All @@ -84,7 +84,7 @@ public void onInventoryOpen(@NotNull InventoryOpenEvent event) {
final Player player = (Player) event.getPlayer();
if (!InventoryManager.playerIsLoaded(player)
|| event.getInventory().getType() != InventoryType.WORKBENCH
|| isNotNeededHere(player)) {
|| isExtensionsNotNeededHere(player)) {
return;
}

Expand All @@ -98,7 +98,7 @@ public void onInventoryClick(@NotNull InventoryClickEvent event) {
//noinspection ConstantConditions
if (!InventoryManager.playerIsLoaded(player)
|| event.getInventory().getType() != InventoryType.WORKBENCH
|| isNotNeededHere(player)) {
|| isExtensionsNotNeededHere(player)) {
return;
}

Expand All @@ -114,7 +114,13 @@ public void onInventoryClick(@NotNull InventoryClickEvent event) {
}
}

private boolean isNotNeededHere(Player player) {
/**
* Checks that inventory extensions not needed there.
* It always should be used after `InventoryManager.playerIsLoaded(player)` check.
*
* @param player Player to check
*/
private boolean isExtensionsNotNeededHere(Player player) {
return !InventoryManager.get(player).isPocketCraft()
&& !Config.getConfig().getBoolean("craft.workbench", true);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -262,13 +262,15 @@ public void onInventoryClick(@NotNull final InventoryClickEvent event) {

// Crafting area
if (inventory.getType() == InventoryType.CRAFTING) {
if (InventoryManager.get(player).isOpened()) {
PlayerWrapper playerWrapper = InventoryManager.get(player);

if (playerWrapper.isOpened()) {
return;
}

switch (event.getSlotType()) {
case CRAFTING:
InventoryManager.get(player).openInventory(true);
playerWrapper.openInventory(true);
case QUICKBAR:
// Shield slot is QUICKBAR and has rawId - 45 o.O
if (rawSlot != 45) {
Expand Down Expand Up @@ -491,10 +493,8 @@ public void onWorldChanged(@NotNull PlayerChangedWorldEvent event) {

if (!InventoryManager.isAllowedWorld(player.getWorld())) {
InventoryManager.unloadPlayerInventory(player);
} else {
if (InventoryManager.get(player).hasPet()) {
PetManager.respawnPet(player);
}
} else if (InventoryManager.get(player).hasPet()) {
PetManager.respawnPet(player);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,32 +18,21 @@

package ru.endlesscode.rpginventory.event.listener;

import org.bukkit.GameMode;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.OfflinePlayer;
import org.bukkit.Sound;
import org.bukkit.*;
import org.bukkit.entity.*;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.block.Action;
import org.bukkit.event.entity.EntityDamageByEntityEvent;
import org.bukkit.event.entity.EntityDeathEvent;
import org.bukkit.event.entity.EntityPortalEnterEvent;
import org.bukkit.event.entity.EntityTargetLivingEntityEvent;
import org.bukkit.event.entity.PlayerDeathEvent;
import org.bukkit.event.entity.*;
import org.bukkit.event.inventory.InventoryOpenEvent;
import org.bukkit.event.player.PlayerInteractEntityEvent;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.event.player.PlayerMoveEvent;
import org.bukkit.event.player.PlayerRespawnEvent;
import org.bukkit.event.player.*;
import org.bukkit.event.vehicle.VehicleEnterEvent;
import org.bukkit.inventory.HorseInventory;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import org.bukkit.scheduler.BukkitRunnable;
import org.jetbrains.annotations.*;
import org.jetbrains.annotations.NotNull;
import ru.endlesscode.rpginventory.RPGInventory;
import ru.endlesscode.rpginventory.inventory.InventoryManager;
import ru.endlesscode.rpginventory.inventory.PlayerWrapper;
Expand Down Expand Up @@ -119,6 +108,39 @@ public void onPlayerRespawn(@NotNull PlayerRespawnEvent event) {
}
}

//Possible fix #110
@EventHandler(ignoreCancelled = true, priority = EventPriority.HIGH)
public void onPlayerTeleport(@NotNull PlayerTeleportEvent event) {
Player player = event.getPlayer();

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

if (!InventoryManager.get(player).hasPet()) {
return;
}

//Ugly trick to avoid infinite pet spawning when player teleports from non-solid/non-cuboid block
final Location from = event.getFrom();
final Location to = event.getTo();
if (from.getBlockX() != to.getBlockX() || from.getBlockZ() != to.getBlockZ()) {
return;
}
if (from.distance(to) < 0.775D) {
return;
}

final double maxDistance = (event.getPlayer().getServer().getViewDistance() / 2.0D) * 15.75D;
final ItemStack item = InventoryManager.get(player).getInventory().getItem(PetManager.getPetSlotId());
if (from.distance(to) > maxDistance && item != null) {
PetManager.spawnPet(player, item);
} else {
PetManager.teleportPet(player, to);
}

}

@EventHandler
public void onPetFeed(@NotNull PlayerInteractEntityEvent event) {
Player player = event.getPlayer();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

package ru.endlesscode.rpginventory.inventory;

import com.comphenix.protocol.*;
import org.bukkit.Material;
import org.bukkit.OfflinePlayer;
import org.bukkit.World;
Expand Down Expand Up @@ -52,10 +53,7 @@
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.*;

@SuppressWarnings("ResultOfMethodCallIgnored")
public class InventoryManager {
Expand Down Expand Up @@ -549,14 +547,13 @@ public static void savePlayerInventory(@NotNull Player player) {
}
}

public static PlayerWrapper get(@Nullable OfflinePlayer player) {
if (player == null) {
throw new IllegalArgumentException("OfflinePlayer can not be null!");
}
@NotNull
public static PlayerWrapper get(@NotNull OfflinePlayer player) {
PlayerWrapper playerWrapper = InventoryManager.INVENTORIES.get(player.getUniqueId());
if (playerWrapper == null) {
throw new IllegalArgumentException(player.getName() + "'s inventory is not loaded!");
throw new IllegalStateException("" + player.getName() + "'s inventory should be initialized!");
}

return playerWrapper;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.logging.Level;

/**
* Created by OsipXD on 05.09.2015
Expand Down Expand Up @@ -79,7 +80,7 @@ public static boolean init() {
try {
SlotManager.slotManager = new SlotManager();
} catch (Exception e) {
e.printStackTrace();
RPGInventory.getPluginLogger().log(Level.WARNING, "Failed to initialize SlotManager", e);
return false;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,12 @@
* It is part of the RpgInventory.
* All rights reserved 2014 - 2016 © «EndlessCode Group»
*/
@Deprecated
class CooldownTimer extends BukkitRunnable {
private final Player player;
private final ItemStack petItem;

@Deprecated
public CooldownTimer(Player player, ItemStack petItem) {
this.player = player;
this.petItem = petItem;
Expand Down
133 changes: 133 additions & 0 deletions src/main/java/ru/endlesscode/rpginventory/pet/CooldownsTimer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
package ru.endlesscode.rpginventory.pet;

import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.scheduler.BukkitRunnable;
import ru.endlesscode.rpginventory.RPGInventory;
import ru.endlesscode.rpginventory.inventory.InventoryManager;
import ru.endlesscode.rpginventory.inventory.slot.Slot;
import ru.endlesscode.rpginventory.inventory.slot.SlotManager;
import ru.endlesscode.rpginventory.utils.ItemUtils;

import java.util.*;
import java.util.concurrent.atomic.AtomicInteger;

/**
* Created by Dereku on 17.04.2018
* It is part of the RpgInventory.
* All rights reserved 2014 - 2018 © «EndlessCode Group»
*/
public class CooldownsTimer extends BukkitRunnable {

public final static int TICK_PERIOD = 5;
private final static int TICK_RATE = 20 / CooldownsTimer.TICK_PERIOD;
//temporaryMap for avoid CME.
private final HashMap<UUID, ValuePair> petItemsByPlayer = new HashMap<>(), temporaryMap = new HashMap<>();
private final RPGInventory plugin;
private final Slot petSlot;

@SuppressWarnings("WeakerAccess")
public CooldownsTimer(RPGInventory pluginInstance) {
this.plugin = pluginInstance;
this.petSlot = Objects.requireNonNull(SlotManager.instance().getPetSlot(), "Pet slot can't be null!");
}

public void addPetCooldown(Player player, ItemStack itemStack) {
if (player == null || itemStack == null || itemStack.getType() == Material.AIR) {
//throw new IllegalArgumentException?
return;
}

if (PetManager.getPetFromItem(itemStack) == null) {
//throw new IllegalArgumentException?
return;
}

this.temporaryMap.put(player.getUniqueId(), new ValuePair(itemStack, new AtomicInteger(0)));

}

@Override
public void run() {
//Because there is no safe way to iterate map.
if (!this.temporaryMap.isEmpty()) {
this.petItemsByPlayer.putAll(this.temporaryMap);
this.temporaryMap.clear();
}

final Iterator<Map.Entry<UUID, ValuePair>> iterator = this.petItemsByPlayer.entrySet().iterator();
while (iterator.hasNext()) {
final Map.Entry<UUID, ValuePair> next = iterator.next();
int ticks = next.getValue().getTimer().incrementAndGet();

if (ticks % CooldownsTimer.TICK_RATE != 0) {
continue;
}

final Player player = this.plugin.getServer().getPlayer(next.getKey());
if (player == null || !InventoryManager.playerIsLoaded(player)) {
iterator.remove();
continue;
}

final Inventory inventory = InventoryManager.get(player).getInventory();
if (inventory == null || inventory.getItem(PetManager.getPetSlotId()) == null) {
iterator.remove();
continue;
}

//Future bug fix of multiple pets cooldown
//TODO: Improve that. If we need it, ofc.
/*if (!inventory.getItem(PetManager.getPetSlotId()).isSimilar(next.getValue().getItemStack())) {
iterator.remove();
continue;
}*/

int cooldown = PetManager.getCooldown(next.getValue().getItemStack());
if (1 > cooldown) {
PetManager.saveDeathTime(next.getValue().getItemStack(), 0);
PetManager.spawnPet(player, next.getValue().getItemStack());
inventory.setItem(PetManager.getPetSlotId(), next.getValue().getItemStack());
iterator.remove();
} else if (60 >= cooldown) {
final ItemStack item = next.getValue().getItemStack().clone();
final ItemMeta itemMeta = item.getItemMeta();
itemMeta.setDisplayName(
itemMeta.getDisplayName() + RPGInventory.getLanguage().getMessage("pet.cooldown", cooldown)
);
item.setItemMeta(itemMeta);
PetManager.addGlow(item);

String itemTag = ItemUtils.getTag(item, ItemUtils.PET_TAG);
if (itemTag != null) {
ItemUtils.setTag(item, ItemUtils.PET_TAG, itemTag);
inventory.setItem(PetManager.getPetSlotId(), item);
} else {
inventory.setItem(PetManager.getPetSlotId(), this.petSlot.getCup());
iterator.remove();
}
}
}
}

private class ValuePair {
private final ItemStack itemStack;
private final AtomicInteger timer;

private ValuePair(ItemStack itemStack, AtomicInteger timer) {
this.itemStack = itemStack;
this.timer = timer;
}

private ItemStack getItemStack() {
return itemStack;
}

private AtomicInteger getTimer() {
return timer;
}
}
}
Loading

0 comments on commit a7a35e0

Please sign in to comment.