mirror of
https://github.com/hyperdefined/ToolStats.git
synced 2026-01-29 00:36:01 +00:00
support for adding lore to minecart items
This commit is contained in:
@@ -23,7 +23,9 @@ import org.bukkit.Bukkit;
|
|||||||
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.Chest;
|
||||||
import org.bukkit.entity.Player;
|
import org.bukkit.entity.Player;
|
||||||
|
import org.bukkit.entity.minecart.StorageMinecart;
|
||||||
import org.bukkit.event.EventHandler;
|
import org.bukkit.event.EventHandler;
|
||||||
import org.bukkit.event.Listener;
|
import org.bukkit.event.Listener;
|
||||||
import org.bukkit.event.world.LootGenerateEvent;
|
import org.bukkit.event.world.LootGenerateEvent;
|
||||||
@@ -55,45 +57,71 @@ public class GenerateLoot implements Listener {
|
|||||||
}
|
}
|
||||||
Location lootLocation = event.getLootContext().getLocation();
|
Location lootLocation = event.getLootContext().getLocation();
|
||||||
Inventory chestInv = inventoryHolder.getInventory();
|
Inventory chestInv = inventoryHolder.getInventory();
|
||||||
Block openedChest = null;
|
|
||||||
// look at the current list of opened chest and get the distance
|
|
||||||
// between the lootcontext location and chest location
|
|
||||||
// if the distance is less than 1, it's the same chest
|
|
||||||
for (Block chest : toolStats.playerInteract.openedChests.keySet()) {
|
|
||||||
Location chestLocation = chest.getLocation();
|
|
||||||
double distance = lootLocation.distance(chestLocation);
|
|
||||||
if (distance <= 1.0) {
|
|
||||||
openedChest = chest;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// ignore if the chest is not in the same location
|
|
||||||
if (openedChest == null) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// run task later since if it runs on the same tick it breaks idk
|
if (inventoryHolder instanceof Chest) {
|
||||||
Block finalOpenedChest = openedChest;
|
Block openedChest = null;
|
||||||
Bukkit.getScheduler().runTaskLater(toolStats, () -> {
|
// look at the current list of opened chest and get the distance
|
||||||
Player player = toolStats.playerInteract.openedChests.get(finalOpenedChest);
|
// between the lootcontext location and chest location
|
||||||
// do a classic for loop, so we keep track of chest index of item
|
// if the distance is less than 1, it's the same chest
|
||||||
for (int i = 0; i < chestInv.getContents().length; i++) {
|
for (Block chest : toolStats.playerInteract.openedChests.keySet()) {
|
||||||
ItemStack itemStack = chestInv.getItem(i);
|
Location chestLocation = chest.getLocation();
|
||||||
// ignore air
|
double distance = lootLocation.distance(chestLocation);
|
||||||
if (itemStack == null || itemStack.getType() == Material.AIR) {
|
if (distance <= 1.0) {
|
||||||
continue;
|
openedChest = chest;
|
||||||
}
|
}
|
||||||
String name = itemStack.getType().toString().toLowerCase(Locale.ROOT);
|
}
|
||||||
for (String x : toolStats.allValidItems) {
|
// ignore if the chest is not in the same location
|
||||||
if (name.contains(x)) {
|
if (openedChest == null) {
|
||||||
ItemStack newItem = addLore(itemStack, player);
|
return;
|
||||||
if (newItem != null) {
|
}
|
||||||
chestInv.setItem(i, newItem);
|
|
||||||
|
// run task later since if it runs on the same tick it breaks idk
|
||||||
|
Block finalOpenedChest = openedChest;
|
||||||
|
Bukkit.getScheduler().runTaskLater(toolStats, () -> {
|
||||||
|
Player player = toolStats.playerInteract.openedChests.get(finalOpenedChest);
|
||||||
|
// do a classic for loop, so we keep track of chest index of item
|
||||||
|
for (int i = 0; i < chestInv.getContents().length; i++) {
|
||||||
|
ItemStack itemStack = chestInv.getItem(i);
|
||||||
|
// ignore air
|
||||||
|
if (itemStack == null || itemStack.getType() == Material.AIR) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
String name = itemStack.getType().toString().toLowerCase(Locale.ROOT);
|
||||||
|
for (String x : toolStats.allValidItems) {
|
||||||
|
if (name.contains(x)) {
|
||||||
|
ItemStack newItem = addLore(itemStack, player);
|
||||||
|
if (newItem != null) {
|
||||||
|
chestInv.setItem(i, newItem);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}, 1);
|
||||||
|
}
|
||||||
|
if (inventoryHolder instanceof StorageMinecart) {
|
||||||
|
StorageMinecart mineCart = (StorageMinecart) inventoryHolder;
|
||||||
|
if (toolStats.playerInteract.openedMineCarts.containsKey(mineCart)) {
|
||||||
|
Player player = toolStats.playerInteract.openedMineCarts.get(mineCart);
|
||||||
|
// player clicked this minecart
|
||||||
|
for (int i = 0; i < chestInv.getContents().length; i++) {
|
||||||
|
ItemStack itemStack = chestInv.getItem(i);
|
||||||
|
// ignore air
|
||||||
|
if (itemStack == null || itemStack.getType() == Material.AIR) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
String name = itemStack.getType().toString().toLowerCase(Locale.ROOT);
|
||||||
|
for (String x : toolStats.allValidItems) {
|
||||||
|
if (name.contains(x)) {
|
||||||
|
ItemStack newItem = addLore(itemStack, player);
|
||||||
|
if (newItem != null) {
|
||||||
|
chestInv.setItem(i, newItem);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}, 1);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -22,10 +22,14 @@ import org.bukkit.Bukkit;
|
|||||||
import org.bukkit.GameMode;
|
import org.bukkit.GameMode;
|
||||||
import org.bukkit.Material;
|
import org.bukkit.Material;
|
||||||
import org.bukkit.block.Block;
|
import org.bukkit.block.Block;
|
||||||
|
import org.bukkit.entity.Entity;
|
||||||
|
import org.bukkit.entity.EntityType;
|
||||||
import org.bukkit.entity.Player;
|
import org.bukkit.entity.Player;
|
||||||
|
import org.bukkit.entity.minecart.StorageMinecart;
|
||||||
import org.bukkit.event.EventHandler;
|
import org.bukkit.event.EventHandler;
|
||||||
import org.bukkit.event.Listener;
|
import org.bukkit.event.Listener;
|
||||||
import org.bukkit.event.block.Action;
|
import org.bukkit.event.block.Action;
|
||||||
|
import org.bukkit.event.player.PlayerInteractEntityEvent;
|
||||||
import org.bukkit.event.player.PlayerInteractEvent;
|
import org.bukkit.event.player.PlayerInteractEvent;
|
||||||
|
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
@@ -35,6 +39,7 @@ public class PlayerInteract implements Listener {
|
|||||||
private final ToolStats toolStats;
|
private final ToolStats toolStats;
|
||||||
|
|
||||||
public final HashMap<Block, Player> openedChests = new HashMap<>();
|
public final HashMap<Block, Player> openedChests = new HashMap<>();
|
||||||
|
public final HashMap<StorageMinecart, Player> openedMineCarts = new HashMap<>();
|
||||||
|
|
||||||
public PlayerInteract(ToolStats toolStats) {
|
public PlayerInteract(ToolStats toolStats) {
|
||||||
this.toolStats = toolStats;
|
this.toolStats = toolStats;
|
||||||
@@ -56,11 +61,24 @@ public class PlayerInteract implements Listener {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
// store when a player opens a chest
|
// store when a player opens a chest
|
||||||
// this is used to detect who opens a newly spawned chest
|
|
||||||
// since that is not really tracked on the lootevent
|
|
||||||
if (block.getType() != Material.AIR && block.getType() == Material.CHEST) {
|
if (block.getType() != Material.AIR && block.getType() == Material.CHEST) {
|
||||||
openedChests.put(block, player);
|
openedChests.put(block, player);
|
||||||
Bukkit.getScheduler().runTaskLater(toolStats, () -> openedChests.remove(block), 20);
|
Bukkit.getScheduler().runTaskLater(toolStats, () -> openedChests.remove(block), 20);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@EventHandler
|
||||||
|
public void onInteract(PlayerInteractEntityEvent event) {
|
||||||
|
Entity clicked = event.getRightClicked();
|
||||||
|
Player player = event.getPlayer();
|
||||||
|
if (player.getGameMode() == GameMode.CREATIVE || player.getGameMode() == GameMode.SPECTATOR) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
// store when a player opens a minecart
|
||||||
|
if (clicked.getType() == EntityType.MINECART_CHEST) {
|
||||||
|
StorageMinecart storageMinecart = (StorageMinecart) clicked;
|
||||||
|
openedMineCarts.put(storageMinecart, player);
|
||||||
|
Bukkit.getScheduler().runTaskLater(toolStats, () -> openedMineCarts.remove(storageMinecart), 20);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user