Skip to content

Commit d06a1c4

Browse files
committed
support for adding lore to minecart items
1 parent e22fc7b commit d06a1c4

File tree

2 files changed

+80
-34
lines changed

2 files changed

+80
-34
lines changed

src/main/java/lol/hyper/toolstats/events/GenerateLoot.java

Lines changed: 60 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@
2323
import org.bukkit.Location;
2424
import org.bukkit.Material;
2525
import org.bukkit.block.Block;
26+
import org.bukkit.block.Chest;
2627
import org.bukkit.entity.Player;
28+
import org.bukkit.entity.minecart.StorageMinecart;
2729
import org.bukkit.event.EventHandler;
2830
import org.bukkit.event.Listener;
2931
import org.bukkit.event.world.LootGenerateEvent;
@@ -55,45 +57,71 @@ public void onGenerateLoot(LootGenerateEvent event) {
5557
}
5658
Location lootLocation = event.getLootContext().getLocation();
5759
Inventory chestInv = inventoryHolder.getInventory();
58-
Block openedChest = null;
59-
// look at the current list of opened chest and get the distance
60-
// between the lootcontext location and chest location
61-
// if the distance is less than 1, it's the same chest
62-
for (Block chest : toolStats.playerInteract.openedChests.keySet()) {
63-
Location chestLocation = chest.getLocation();
64-
double distance = lootLocation.distance(chestLocation);
65-
if (distance <= 1.0) {
66-
openedChest = chest;
60+
61+
if (inventoryHolder instanceof Chest) {
62+
Block openedChest = null;
63+
// look at the current list of opened chest and get the distance
64+
// between the lootcontext location and chest location
65+
// if the distance is less than 1, it's the same chest
66+
for (Block chest : toolStats.playerInteract.openedChests.keySet()) {
67+
Location chestLocation = chest.getLocation();
68+
double distance = lootLocation.distance(chestLocation);
69+
if (distance <= 1.0) {
70+
openedChest = chest;
71+
}
72+
}
73+
// ignore if the chest is not in the same location
74+
if (openedChest == null) {
75+
return;
6776
}
68-
}
69-
// ignore if the chest is not in the same location
70-
if (openedChest == null) {
71-
return;
72-
}
7377

74-
// run task later since if it runs on the same tick it breaks idk
75-
Block finalOpenedChest = openedChest;
76-
Bukkit.getScheduler().runTaskLater(toolStats, () -> {
77-
Player player = toolStats.playerInteract.openedChests.get(finalOpenedChest);
78-
// do a classic for loop, so we keep track of chest index of item
79-
for (int i = 0; i < chestInv.getContents().length; i++) {
80-
ItemStack itemStack = chestInv.getItem(i);
81-
// ignore air
82-
if (itemStack == null || itemStack.getType() == Material.AIR) {
83-
continue;
78+
// run task later since if it runs on the same tick it breaks idk
79+
Block finalOpenedChest = openedChest;
80+
Bukkit.getScheduler().runTaskLater(toolStats, () -> {
81+
Player player = toolStats.playerInteract.openedChests.get(finalOpenedChest);
82+
// do a classic for loop, so we keep track of chest index of item
83+
for (int i = 0; i < chestInv.getContents().length; i++) {
84+
ItemStack itemStack = chestInv.getItem(i);
85+
// ignore air
86+
if (itemStack == null || itemStack.getType() == Material.AIR) {
87+
continue;
88+
}
89+
String name = itemStack.getType().toString().toLowerCase(Locale.ROOT);
90+
for (String x : toolStats.allValidItems) {
91+
if (name.contains(x)) {
92+
ItemStack newItem = addLore(itemStack, player);
93+
if (newItem != null) {
94+
chestInv.setItem(i, newItem);
95+
}
96+
}
97+
}
8498
}
85-
String name = itemStack.getType().toString().toLowerCase(Locale.ROOT);
86-
for (String x : toolStats.allValidItems) {
87-
if (name.contains(x)) {
88-
ItemStack newItem = addLore(itemStack, player);
89-
if (newItem != null) {
90-
chestInv.setItem(i, newItem);
99+
100+
}, 1);
101+
}
102+
if (inventoryHolder instanceof StorageMinecart) {
103+
StorageMinecart mineCart = (StorageMinecart) inventoryHolder;
104+
if (toolStats.playerInteract.openedMineCarts.containsKey(mineCart)) {
105+
Player player = toolStats.playerInteract.openedMineCarts.get(mineCart);
106+
// player clicked this minecart
107+
for (int i = 0; i < chestInv.getContents().length; i++) {
108+
ItemStack itemStack = chestInv.getItem(i);
109+
// ignore air
110+
if (itemStack == null || itemStack.getType() == Material.AIR) {
111+
continue;
112+
}
113+
String name = itemStack.getType().toString().toLowerCase(Locale.ROOT);
114+
for (String x : toolStats.allValidItems) {
115+
if (name.contains(x)) {
116+
ItemStack newItem = addLore(itemStack, player);
117+
if (newItem != null) {
118+
chestInv.setItem(i, newItem);
119+
}
91120
}
92121
}
93122
}
94123
}
95-
96-
}, 1);
124+
}
97125
}
98126

99127
/**

src/main/java/lol/hyper/toolstats/events/PlayerInteract.java

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,14 @@
2222
import org.bukkit.GameMode;
2323
import org.bukkit.Material;
2424
import org.bukkit.block.Block;
25+
import org.bukkit.entity.Entity;
26+
import org.bukkit.entity.EntityType;
2527
import org.bukkit.entity.Player;
28+
import org.bukkit.entity.minecart.StorageMinecart;
2629
import org.bukkit.event.EventHandler;
2730
import org.bukkit.event.Listener;
2831
import org.bukkit.event.block.Action;
32+
import org.bukkit.event.player.PlayerInteractEntityEvent;
2933
import org.bukkit.event.player.PlayerInteractEvent;
3034

3135
import java.util.HashMap;
@@ -35,6 +39,7 @@ public class PlayerInteract implements Listener {
3539
private final ToolStats toolStats;
3640

3741
public final HashMap<Block, Player> openedChests = new HashMap<>();
42+
public final HashMap<StorageMinecart, Player> openedMineCarts = new HashMap<>();
3843

3944
public PlayerInteract(ToolStats toolStats) {
4045
this.toolStats = toolStats;
@@ -56,11 +61,24 @@ public void onInteract(PlayerInteractEvent event) {
5661
return;
5762
}
5863
// store when a player opens a chest
59-
// this is used to detect who opens a newly spawned chest
60-
// since that is not really tracked on the lootevent
6164
if (block.getType() != Material.AIR && block.getType() == Material.CHEST) {
6265
openedChests.put(block, player);
6366
Bukkit.getScheduler().runTaskLater(toolStats, () -> openedChests.remove(block), 20);
6467
}
6568
}
69+
70+
@EventHandler
71+
public void onInteract(PlayerInteractEntityEvent event) {
72+
Entity clicked = event.getRightClicked();
73+
Player player = event.getPlayer();
74+
if (player.getGameMode() == GameMode.CREATIVE || player.getGameMode() == GameMode.SPECTATOR) {
75+
return;
76+
}
77+
// store when a player opens a minecart
78+
if (clicked.getType() == EntityType.MINECART_CHEST) {
79+
StorageMinecart storageMinecart = (StorageMinecart) clicked;
80+
openedMineCarts.put(storageMinecart, player);
81+
Bukkit.getScheduler().runTaskLater(toolStats, () -> openedMineCarts.remove(storageMinecart), 20);
82+
}
83+
}
6684
}

0 commit comments

Comments
 (0)