Skip to content

Commit 850414b

Browse files
committed
Fix #2, move mob out of the block.
Mob is moved out of the block, based on their bounding box and what block face is interacted with. Visual bug where the mob appears within the block first, then is moved.
1 parent ede2e5d commit 850414b

File tree

1 file changed

+26
-3
lines changed

1 file changed

+26
-3
lines changed

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

+26-3
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@
77
import adhdmc.simplebucketmobs.util.Permission;
88
import com.mojang.brigadier.exceptions.CommandSyntaxException;
99
import net.kyori.adventure.text.Component;
10-
import net.kyori.adventure.text.minimessage.MiniMessage;
1110
import net.kyori.adventure.text.minimessage.tag.resolver.Placeholder;
1211
import net.minecraft.nbt.CompoundTag;
1312
import net.minecraft.nbt.Tag;
1413
import net.minecraft.nbt.TagParser;
1514
import org.bukkit.*;
15+
import org.bukkit.block.BlockFace;
1616
import org.bukkit.craftbukkit.v1_19_R2.entity.CraftLivingEntity;
1717
import org.bukkit.entity.*;
1818
import org.bukkit.event.EventHandler;
@@ -27,6 +27,7 @@
2727
import org.bukkit.inventory.meta.ItemMeta;
2828
import org.bukkit.persistence.PersistentDataContainer;
2929
import org.bukkit.persistence.PersistentDataType;
30+
import org.bukkit.util.BoundingBox;
3031

3132
import java.io.IOException;
3233

@@ -98,14 +99,15 @@ public void unbucketMob(PlayerInteractEvent event) {
9899
if (event.getHand() != EquipmentSlot.HAND) return;
99100
Location interactLoc = event.getInteractionPoint();
100101
if (interactLoc == null) return;
102+
101103
Player player = event.getPlayer();
102104
ItemStack bucket = player.getEquipment().getItemInMainHand();
103105
if (bucket.getType() != Material.BUCKET) return;
104106
if (!bucket.getItemMeta().getPersistentDataContainer().has(mobNBTKey)) return;
105107

106108
String serializedNbt = bucket.getItemMeta().getPersistentDataContainer().get(mobNBTKey, PersistentDataType.STRING);
107109

108-
try { if (serializedNbt != null) applyNBT(interactLoc, serializedNbt); }
110+
try { if (serializedNbt != null) applyNBT(interactLoc, serializedNbt, event.getBlockFace()); }
109111
catch (IOException | CommandSyntaxException e) {
110112
player.sendMessage(Message.ERROR_FAILED_DESERIALIZATION.getParsedMessage());
111113
e.printStackTrace();
@@ -155,14 +157,15 @@ private CompoundTag serializeNBT(LivingEntity e) {
155157
* @exception IOException Failed to read NBT Tags.
156158
* @exception CommandSyntaxException What.
157159
*/
158-
private void applyNBT(Location location, String serializedNbt) throws IllegalArgumentException, IOException, CommandSyntaxException {
160+
private void applyNBT(Location location, String serializedNbt, BlockFace face) throws IllegalArgumentException, IOException, CommandSyntaxException {
159161
CompoundTag tag = TagParser.parseTag(serializedNbt);
160162
Tag idTag = tag.get("id");
161163
// TODO: Maybe throw exception.
162164
if (idTag == null) return;
163165
String id = idTag.getAsString().split(":")[1].toUpperCase();
164166
EntityType mobType = EntityType.valueOf(id);
165167
Entity entity = location.getWorld().spawnEntity(location, mobType, CreatureSpawnEvent.SpawnReason.CUSTOM);
168+
entity.teleport(adjustLoc(location, face, entity.getBoundingBox()));
166169
CompoundTag newLoc = new CompoundTag();
167170
((CraftLivingEntity) entity).getHandle().save(newLoc);
168171
tag.put("Motion", newLoc.get("Motion"));
@@ -196,4 +199,24 @@ else if (toUpper) {
196199
return nameCased.toString();
197200
}
198201

202+
/**
203+
* Adjusts the location given
204+
* @param interactionPoint Location of Interaction Point
205+
* @param face Block Face of Interaction
206+
* @return The same Location.
207+
*/
208+
private Location adjustLoc(Location interactionPoint, BlockFace face, BoundingBox entityBox) {
209+
double height = entityBox.getHeight();
210+
double widthX = entityBox.getWidthX();
211+
double widthZ = entityBox.getWidthZ();
212+
switch (face) {
213+
case DOWN -> interactionPoint.add(0, -1*height, 0);
214+
case EAST -> interactionPoint.add(.5*widthX, 0, 0);
215+
case WEST -> interactionPoint.add(-.5*widthX, 0, 0);
216+
case NORTH -> interactionPoint.add(0, 0, -.5*widthZ);
217+
case SOUTH -> interactionPoint.add(0, 0, .5*widthZ);
218+
}
219+
return interactionPoint;
220+
}
221+
199222
}

0 commit comments

Comments
 (0)