-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDropNames.java
170 lines (140 loc) · 4.44 KB
/
DropNames.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
package dk.muj.dropnames;
import com.massivecraft.massivecore.MassivePlugin;
import com.massivecraft.massivecore.util.Txt;
import dk.muj.dropnames.entity.MConf;
import dk.muj.dropnames.entity.MConfColl;
import dk.muj.dropnames.entity.migrator.MigratorMConf001Renames;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.entity.Entity;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.ExperienceOrb;
import org.bukkit.entity.Item;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.entity.EntityDeathEvent;
import org.bukkit.event.entity.ExpBottleEvent;
import org.bukkit.event.entity.ItemMergeEvent;
import org.bukkit.event.entity.ItemSpawnEvent;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
public class DropNames extends MassivePlugin
{
// -------------------------------------------- //
// INSTANCE & CONSTRUCT
// -------------------------------------------- //
private static DropNames i;
public static DropNames get() { return i; }
public DropNames()
{
i = this;
this.setVersionSynchronized(false);
}
// -------------------------------------------- //
// OVERRIDE: PLUGIN
// -------------------------------------------- //
@Override
public void onEnableInner()
{
activate(
MConfColl.class,
MigratorMConf001Renames.class
);
}
// -------------------------------------------- //
// LISTENER: ITEM
// -------------------------------s------------- //
@EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true)
public void itemSpawn(ItemSpawnEvent event)
{
setName(event.getEntity());
}
@EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true)
public void itemMerge(ItemMergeEvent event)
{
Item item = event.getTarget();
Bukkit.getScheduler().runTaskLater(this, () -> setName(item), 1L);
}
public static String getCustomNameItem(Item item)
{
if (!MConf.get().isEnabledItem) return null;
ItemStack stack = item.getItemStack();
String ret;
ItemMeta meta = stack.getItemMeta();
if (meta.hasDisplayName() && MConf.get().displayItemRenamed)
{
ret = meta.getDisplayName();
}
else if (MConf.get().displayItemNotRenamed)
{
String rename = MConf.get().customNames.get(stack.getType());
if (rename != null)
{
ret = rename;
}
else
{
ret = Txt.getMaterialName(stack.getType());
}
ret = Txt.parse(ret);
}
else
{
return null;
}
if (stack.getAmount() > 1 && MConf.get().displayItemAmount)
{
ret += " x" + String.valueOf(stack.getAmount());
}
return ret;
}
// -------------------------------------------- //
// LISTENER: EXPERIENCE ORB
// -------------------------------------------- //
@EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true)
public void entityDeath(EntityDeathEvent event)
{
updateOrbsNearby(event.getEntity().getLocation());
}
@EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true)
public void thrownBottle(ExpBottleEvent event)
{
updateOrbsNearby(event.getEntity().getLocation());
}
public void updateOrbsNearby(Location location)
{
Runnable runnable = () -> {
EntityType type = EntityType.EXPERIENCE_ORB;
location.getWorld().getNearbyEntities(location, 10.0, 10.0, 10.0, entity -> entity.getType() == type).stream().forEach(DropNames::setName);
};
Bukkit.getScheduler().scheduleSyncDelayedTask(this, runnable, 1L);
Bukkit.getScheduler().scheduleSyncDelayedTask(this, runnable, 40L);
}
public static String getCustomNameOrb(ExperienceOrb orb)
{
if (!MConf.get().isEnabledExperienceOrb) return null;
int amount = orb.getExperience();
String ret = MConf.get().experienceOrbName;
if (MConf.get().displayExperienceOrbAmount) ret += " x" + String.valueOf(amount);
ret = Txt.parse(ret);
return ret;
}
// -------------------------------------------- //
// NAMING
// -------------------------------------------- //
public static void setName(Entity entity)
{
if (!MConf.get().isEnabled) return;
if (!MConf.get().worldsEnabled.contains(entity.getLocation().getWorld())) return;
String name = getCustomName(entity);
if (name == null) return;
entity.setCustomName(name);
entity.setCustomNameVisible(true);
}
public static String getCustomName(Entity entity)
{
if (entity instanceof Item) return getCustomNameItem((Item) entity);
if (entity instanceof ExperienceOrb) return getCustomNameOrb((ExperienceOrb) entity);
throw new RuntimeException();
}
}