1
1
package simplexity .simplebucketmobs .listener ;
2
2
3
+ import org .bukkit .craftbukkit .entity .CraftLivingEntity ;
3
4
import simplexity .simplebucketmobs .SimpleBucketMobs ;
4
5
import simplexity .simplebucketmobs .config .Config ;
5
6
import simplexity .simplebucketmobs .config .Texture ;
14
15
import org .bukkit .*;
15
16
import org .bukkit .block .Block ;
16
17
import org .bukkit .block .BlockFace ;
17
- import org . bukkit . craftbukkit . v1_20_R1 . entity . CraftLivingEntity ;
18
+
18
19
import org .bukkit .entity .*;
19
20
import org .bukkit .event .EventHandler ;
20
21
import org .bukkit .event .EventPriority ;
@@ -47,13 +48,13 @@ public void bucketMob(PlayerInteractEntityEvent event) {
47
48
if (!(clicked instanceof LivingEntity entity )) return ;
48
49
if (entity .getType () == EntityType .PLAYER ) return ;
49
50
if (Config .getInstance ().isNoHostileTargeting ()
50
- && entity instanceof Monster monster
51
- && monster .getTarget () != null
52
- && monster .getTarget ().equals (player )) {
51
+ && entity instanceof Monster monster
52
+ && monster .getTarget () != null
53
+ && monster .getTarget ().equals (player )) {
53
54
player .sendMessage (Message .ERROR_BUCKET_HOSTILE_TARGETING .getParsedMessage ());
54
55
return ;
55
56
}
56
- if (!Config .getInstance ().getAllowedTypes ().contains (entity .getType ())) return ;
57
+ if (!Config .getInstance ().getAllowedBasicTypes ().contains (entity .getType ())) return ;
57
58
if (!(player .hasPermission (Permission .BUCKET_ALL .get ()) || player .hasPermission (Permission .BUCKET_MOB .get () + entity .getType ()))) {
58
59
player .sendMessage (Message .ERROR_BUCKET_NO_PERMISSION .getParsedMessage ());
59
60
return ;
@@ -67,7 +68,6 @@ public void bucketMob(PlayerInteractEntityEvent event) {
67
68
event .setCancelled (true );
68
69
return ;
69
70
}
70
-
71
71
ItemStack mobBucket = new ItemStack (Material .BUCKET );
72
72
Entity vehicle = entity .getVehicle ();
73
73
if (vehicle != null ) vehicle .removePassenger (entity );
@@ -91,12 +91,17 @@ public void bucketMob(PlayerInteractEntityEvent event) {
91
91
Texture .getInstance ().setCustomData (entity .getType (), meta , tag );
92
92
mobBucket .setItemMeta (meta );
93
93
if (player .getGameMode () != GameMode .CREATIVE ) bucket .subtract ();
94
- player .getInventory ().addItem (mobBucket );
94
+ if (player .getInventory ().firstEmpty () == -1 ) {
95
+ player .getWorld ().dropItemNaturally (player .getLocation (), mobBucket );
96
+ } else {
97
+ player .getInventory ().addItem (mobBucket );
98
+ }
95
99
try {
96
100
// TODO: Configurable?
97
101
String soundName = "ENTITY_" + entity .getType () + "_HURT" ;
98
102
player .playSound (player .getLocation (), Sound .valueOf (soundName ), 0.75f , 1.0f );
99
- } catch (IllegalArgumentException ignored ) { }
103
+ } catch (IllegalArgumentException ignored ) {
104
+ }
100
105
entity .remove ();
101
106
}
102
107
@@ -115,13 +120,13 @@ public void unbucketMob(PlayerInteractEvent event) {
115
120
116
121
String serializedNbt = bucket .getItemMeta ().getPersistentDataContainer ().get (mobNBTKey , PersistentDataType .STRING );
117
122
118
- try { if (serializedNbt != null ) applyNBT (location , serializedNbt , event .getBlockFace ()); }
119
- catch (IOException | CommandSyntaxException e ) {
123
+ try {
124
+ if (serializedNbt != null ) applyNBT (location , serializedNbt , event .getBlockFace ());
125
+ } catch (IOException | CommandSyntaxException e ) {
120
126
player .sendMessage (Message .ERROR_FAILED_DESERIALIZATION .getParsedMessage ());
121
127
e .printStackTrace ();
122
128
return ;
123
- }
124
- catch (IllegalArgumentException e ) {
129
+ } catch (IllegalArgumentException e ) {
125
130
player .sendMessage (Message .ERROR_NO_BUCKET_MOB .getParsedMessage ());
126
131
e .printStackTrace ();
127
132
return ;
@@ -148,6 +153,7 @@ public void noBucketLiquid(PlayerBucketFillEvent event) {
148
153
149
154
/**
150
155
* Serializes the NBT Data from the LivingEntity.
156
+ *
151
157
* @param e LivingEntity
152
158
* @return String serialization of the LivingEntity.
153
159
*/
@@ -159,22 +165,20 @@ private CompoundTag serializeNBT(LivingEntity e) {
159
165
160
166
/**
161
167
* Deserializes the NBT Data into the LivingEntity.
162
- * @param location Location to spawn Mob.
168
+ *
169
+ * @param location Location to spawn Mob.
163
170
* @param serializedNbt NBT as a String
164
- * @exception IllegalArgumentException Invalid Mob Type Found
165
- * @exception IOException Failed to read NBT Tags.
166
- * @exception CommandSyntaxException What.
171
+ * @throws IllegalArgumentException Invalid Mob Type Found
172
+ * @throws IOException Failed to read NBT Tags.
173
+ * @throws CommandSyntaxException What.
167
174
*/
168
175
private void applyNBT (Location location , String serializedNbt , BlockFace face ) throws IllegalArgumentException , IOException , CommandSyntaxException {
169
176
CompoundTag tag = TagParser .parseTag (serializedNbt );
170
177
Tag idTag = tag .get ("id" );
171
178
// TODO: Maybe throw exception.
172
179
if (idTag == null ) return ;
173
180
String id = idTag .getAsString ().split (":" )[1 ].toUpperCase ();
174
- EntityType mobType ;
175
- // Special cases where minecraft:id != Bukkit Name.
176
- if (id .equals ("MOOSHROOM" )) mobType = EntityType .MUSHROOM_COW ;
177
- else mobType = EntityType .valueOf (id );
181
+ EntityType mobType = EntityType .valueOf (id );
178
182
Entity entity = location .getWorld ().spawnEntity (location , mobType , CreatureSpawnEvent .SpawnReason .CUSTOM , spawned -> {
179
183
CompoundTag newLoc = new CompoundTag ();
180
184
((CraftLivingEntity ) spawned ).getHandle ().save (newLoc );
@@ -189,6 +193,7 @@ private void applyNBT(Location location, String serializedNbt, BlockFace face) t
189
193
190
194
/**
191
195
* Converts a string to Name Case
196
+ *
192
197
* @param input String
193
198
* @return String but using Name Case
194
199
* @implNote Thanks Baeldung (https://www.baeldung.com/java-string-title-case)
@@ -203,8 +208,7 @@ private String nameCase(String input) {
203
208
else if (toUpper ) {
204
209
c = Character .toTitleCase (c );
205
210
toUpper = false ;
206
- }
207
- else {
211
+ } else {
208
212
c = Character .toLowerCase (c );
209
213
}
210
214
nameCased .append (c );
@@ -214,20 +218,21 @@ else if (toUpper) {
214
218
215
219
/**
216
220
* Adjusts the location given
221
+ *
217
222
* @param interactionPoint Location of Interaction Point
218
- * @param face Block Face of Interaction
223
+ * @param face Block Face of Interaction
219
224
* @return The same Location.
220
225
*/
221
226
private Location adjustLoc (Location interactionPoint , BlockFace face , BoundingBox entityBox ) {
222
227
double height = entityBox .getHeight ();
223
228
double widthX = entityBox .getWidthX ();
224
229
double widthZ = entityBox .getWidthZ ();
225
230
switch (face ) {
226
- case DOWN -> interactionPoint .add (0 , -1 * height , 0 );
227
- case EAST -> interactionPoint .add (.5 * widthX , 0 , 0 );
228
- case WEST -> interactionPoint .add (-.5 * widthX , 0 , 0 );
229
- case NORTH -> interactionPoint .add (0 , 0 , -.5 * widthZ );
230
- case SOUTH -> interactionPoint .add (0 , 0 , .5 * widthZ );
231
+ case DOWN -> interactionPoint .add (0 , -1 * height , 0 );
232
+ case EAST -> interactionPoint .add (.5 * widthX , 0 , 0 );
233
+ case WEST -> interactionPoint .add (-.5 * widthX , 0 , 0 );
234
+ case NORTH -> interactionPoint .add (0 , 0 , -.5 * widthZ );
235
+ case SOUTH -> interactionPoint .add (0 , 0 , .5 * widthZ );
231
236
}
232
237
return interactionPoint ;
233
238
}
0 commit comments