|
7 | 7 | import adhdmc.simplebucketmobs.util.Permission;
|
8 | 8 | import com.mojang.brigadier.exceptions.CommandSyntaxException;
|
9 | 9 | import net.kyori.adventure.text.Component;
|
10 |
| -import net.kyori.adventure.text.minimessage.MiniMessage; |
11 | 10 | import net.kyori.adventure.text.minimessage.tag.resolver.Placeholder;
|
12 | 11 | import net.minecraft.nbt.CompoundTag;
|
13 | 12 | import net.minecraft.nbt.Tag;
|
14 | 13 | import net.minecraft.nbt.TagParser;
|
15 | 14 | import org.bukkit.*;
|
| 15 | +import org.bukkit.block.BlockFace; |
16 | 16 | import org.bukkit.craftbukkit.v1_19_R2.entity.CraftLivingEntity;
|
17 | 17 | import org.bukkit.entity.*;
|
18 | 18 | import org.bukkit.event.EventHandler;
|
|
27 | 27 | import org.bukkit.inventory.meta.ItemMeta;
|
28 | 28 | import org.bukkit.persistence.PersistentDataContainer;
|
29 | 29 | import org.bukkit.persistence.PersistentDataType;
|
| 30 | +import org.bukkit.util.BoundingBox; |
30 | 31 |
|
31 | 32 | import java.io.IOException;
|
32 | 33 |
|
@@ -98,14 +99,15 @@ public void unbucketMob(PlayerInteractEvent event) {
|
98 | 99 | if (event.getHand() != EquipmentSlot.HAND) return;
|
99 | 100 | Location interactLoc = event.getInteractionPoint();
|
100 | 101 | if (interactLoc == null) return;
|
| 102 | + |
101 | 103 | Player player = event.getPlayer();
|
102 | 104 | ItemStack bucket = player.getEquipment().getItemInMainHand();
|
103 | 105 | if (bucket.getType() != Material.BUCKET) return;
|
104 | 106 | if (!bucket.getItemMeta().getPersistentDataContainer().has(mobNBTKey)) return;
|
105 | 107 |
|
106 | 108 | String serializedNbt = bucket.getItemMeta().getPersistentDataContainer().get(mobNBTKey, PersistentDataType.STRING);
|
107 | 109 |
|
108 |
| - try { if (serializedNbt != null) applyNBT(interactLoc, serializedNbt); } |
| 110 | + try { if (serializedNbt != null) applyNBT(interactLoc, serializedNbt, event.getBlockFace()); } |
109 | 111 | catch (IOException | CommandSyntaxException e) {
|
110 | 112 | player.sendMessage(Message.ERROR_FAILED_DESERIALIZATION.getParsedMessage());
|
111 | 113 | e.printStackTrace();
|
@@ -155,14 +157,15 @@ private CompoundTag serializeNBT(LivingEntity e) {
|
155 | 157 | * @exception IOException Failed to read NBT Tags.
|
156 | 158 | * @exception CommandSyntaxException What.
|
157 | 159 | */
|
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 { |
159 | 161 | CompoundTag tag = TagParser.parseTag(serializedNbt);
|
160 | 162 | Tag idTag = tag.get("id");
|
161 | 163 | // TODO: Maybe throw exception.
|
162 | 164 | if (idTag == null) return;
|
163 | 165 | String id = idTag.getAsString().split(":")[1].toUpperCase();
|
164 | 166 | EntityType mobType = EntityType.valueOf(id);
|
165 | 167 | Entity entity = location.getWorld().spawnEntity(location, mobType, CreatureSpawnEvent.SpawnReason.CUSTOM);
|
| 168 | + entity.teleport(adjustLoc(location, face, entity.getBoundingBox())); |
166 | 169 | CompoundTag newLoc = new CompoundTag();
|
167 | 170 | ((CraftLivingEntity) entity).getHandle().save(newLoc);
|
168 | 171 | tag.put("Motion", newLoc.get("Motion"));
|
@@ -196,4 +199,24 @@ else if (toUpper) {
|
196 | 199 | return nameCased.toString();
|
197 | 200 | }
|
198 | 201 |
|
| 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 | + |
199 | 222 | }
|
0 commit comments