Skip to content

Commit 2872cbc

Browse files
Entity Consumer and EnderDragon Fixed
Thanks @BillyGalbreath for the Spawn Entity Consumer type beat. Ender Dragon can now be bucketed. Billy pointed that one out too. Mob "adjust location" removed temporarily, may be used later for a TODO for placement based on bounding box. Debucket now requires you to right click block action. Location related changes for #2, mobs now spawn in the center of the empty block. Maybe don't spawn mobs in the ceiling right now. Kept adjustLoc() for now since I plan on doing bounding box checks when debucketing soon (like not suffocating your mob when they spawn in the ceiling). Co-Authored-By: William Blake Galbreath <[email protected]>
1 parent dc523eb commit 2872cbc

File tree

1 file changed

+20
-13
lines changed

1 file changed

+20
-13
lines changed

src/main/java/adhdmc/simplebucketmobs/listener/BucketMob.java

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,14 @@
1212
import net.minecraft.nbt.Tag;
1313
import net.minecraft.nbt.TagParser;
1414
import org.bukkit.*;
15+
import org.bukkit.block.Block;
1516
import org.bukkit.block.BlockFace;
1617
import org.bukkit.craftbukkit.v1_19_R2.entity.CraftLivingEntity;
1718
import org.bukkit.entity.*;
1819
import org.bukkit.event.EventHandler;
1920
import org.bukkit.event.EventPriority;
2021
import org.bukkit.event.Listener;
22+
import org.bukkit.event.block.Action;
2123
import org.bukkit.event.entity.CreatureSpawnEvent;
2224
import org.bukkit.event.player.PlayerBucketFillEvent;
2325
import org.bukkit.event.player.PlayerInteractEntityEvent;
@@ -40,7 +42,9 @@ public void bucketMob(PlayerInteractEntityEvent event) {
4042
if (event.getHand() != EquipmentSlot.HAND) return;
4143
Player player = event.getPlayer();
4244
if (!player.isSneaking()) return;
43-
if (!(event.getRightClicked() instanceof LivingEntity entity)) return;
45+
Entity clicked = event.getRightClicked();
46+
if (clicked instanceof ComplexEntityPart part) clicked = part.getParent();
47+
if (!(clicked instanceof LivingEntity entity)) return;
4448
if (entity.getType() == EntityType.PLAYER) return;
4549
if (Config.getInstance().isNoHostileTargeting()
4650
&& entity instanceof Monster monster
@@ -99,8 +103,10 @@ public void bucketMob(PlayerInteractEntityEvent event) {
99103
@EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true)
100104
public void unbucketMob(PlayerInteractEvent event) {
101105
if (event.getHand() != EquipmentSlot.HAND) return;
102-
Location interactLoc = event.getInteractionPoint();
103-
if (interactLoc == null) return;
106+
if (event.getAction() != Action.RIGHT_CLICK_BLOCK) return;
107+
Block block = event.getClickedBlock();
108+
if (block == null) return;
109+
Location location = block.getRelative(event.getBlockFace()).getLocation().add(0.5, 0.0, 0.5);
104110

105111
Player player = event.getPlayer();
106112
ItemStack bucket = player.getEquipment().getItemInMainHand();
@@ -109,7 +115,7 @@ public void unbucketMob(PlayerInteractEvent event) {
109115

110116
String serializedNbt = bucket.getItemMeta().getPersistentDataContainer().get(mobNBTKey, PersistentDataType.STRING);
111117

112-
try { if (serializedNbt != null) applyNBT(interactLoc, serializedNbt, event.getBlockFace()); }
118+
try { if (serializedNbt != null) applyNBT(location, serializedNbt, event.getBlockFace()); }
113119
catch (IOException | CommandSyntaxException e) {
114120
player.sendMessage(Message.ERROR_FAILED_DESERIALIZATION.getParsedMessage());
115121
e.printStackTrace();
@@ -169,15 +175,16 @@ private void applyNBT(Location location, String serializedNbt, BlockFace face) t
169175
// Special cases where minecraft:id != Bukkit Name.
170176
if (id.equals("MOOSHROOM")) mobType = EntityType.MUSHROOM_COW;
171177
else mobType = EntityType.valueOf(id);
172-
Entity entity = location.getWorld().spawnEntity(location, mobType, CreatureSpawnEvent.SpawnReason.CUSTOM);
173-
entity.teleport(adjustLoc(location, face, entity.getBoundingBox()));
174-
CompoundTag newLoc = new CompoundTag();
175-
((CraftLivingEntity) entity).getHandle().save(newLoc);
176-
tag.put("Motion", newLoc.get("Motion"));
177-
tag.put("Pos", newLoc.get("Pos"));
178-
tag.put("Rotation", newLoc.get("Rotation"));
179-
tag.put("UUID", newLoc.get("UUID"));
180-
((CraftLivingEntity) entity).getHandle().load(tag);
178+
Entity entity = location.getWorld().spawnEntity(location, mobType, CreatureSpawnEvent.SpawnReason.CUSTOM, spawned -> {
179+
CompoundTag newLoc = new CompoundTag();
180+
((CraftLivingEntity) spawned).getHandle().save(newLoc);
181+
tag.put("Motion", newLoc.get("Motion"));
182+
tag.put("Pos", newLoc.get("Pos"));
183+
tag.put("Rotation", newLoc.get("Rotation"));
184+
tag.put("UUID", newLoc.get("UUID"));
185+
((CraftLivingEntity) spawned).getHandle().load(tag);
186+
});
187+
// TODO: Adjust Entity on Bounding Box -- entity.teleport(adjustLoc(location, face, entity.getBoundingBox()));
181188
}
182189

183190
/**

0 commit comments

Comments
 (0)