Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ancient Pedestal Fixes #3784

Merged
merged 12 commits into from
Jul 12, 2023
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import org.bukkit.Location;
import org.bukkit.Sound;
import org.bukkit.block.Block;
import org.bukkit.entity.ArmorStand;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Item;
import org.bukkit.entity.Player;
Expand All @@ -24,13 +25,15 @@
import io.github.thebusybiscuit.slimefun4.api.items.ItemSpawnReason;
import io.github.thebusybiscuit.slimefun4.api.items.SlimefunItemStack;
import io.github.thebusybiscuit.slimefun4.api.recipes.RecipeType;
import io.github.thebusybiscuit.slimefun4.core.attributes.NotHopperable;
import io.github.thebusybiscuit.slimefun4.core.handlers.BlockBreakHandler;
import io.github.thebusybiscuit.slimefun4.core.handlers.BlockDispenseHandler;
import io.github.thebusybiscuit.slimefun4.implementation.Slimefun;
import io.github.thebusybiscuit.slimefun4.implementation.handlers.SimpleBlockBreakHandler;
import io.github.thebusybiscuit.slimefun4.implementation.items.SimpleSlimefunItem;
import io.github.thebusybiscuit.slimefun4.implementation.listeners.AncientAltarListener;
import io.github.thebusybiscuit.slimefun4.implementation.tasks.AncientAltarTask;
import io.github.thebusybiscuit.slimefun4.utils.ArmorStandUtils;
import io.github.thebusybiscuit.slimefun4.utils.SlimefunUtils;

/**
Expand All @@ -40,13 +43,14 @@
*
* @author Redemption198
* @author TheBusyBiscuit
* @author JustAHuman
*
* @see AncientAltar
* @see AncientAltarListener
* @see AncientAltarTask
*
*/
public class AncientPedestal extends SimpleSlimefunItem<BlockDispenseHandler> {
public class AncientPedestal extends SimpleSlimefunItem<BlockDispenseHandler> implements NotHopperable {

public static final String ITEM_PREFIX = ChatColors.color("&dALTAR &3Probe - &e");

Expand All @@ -63,7 +67,8 @@ public AncientPedestal(ItemGroup itemGroup, SlimefunItemStack item, RecipeType r
@Override
public void onBlockBreak(@Nonnull Block b) {
Optional<Item> entity = getPlacedItem(b);

ArmorStand armorStand = getArmorStand(b, false);

if (entity.isPresent()) {
Item stack = entity.get();

Expand All @@ -73,6 +78,10 @@ public void onBlockBreak(@Nonnull Block b) {
stack.remove();
}
}

if (armorStand != null && armorStand.isValid()) {
armorStand.remove();
}
}
};
}
Expand All @@ -93,6 +102,23 @@ public void onBlockBreak(@Nonnull Block b) {

return Optional.empty();
}

public @Nullable ArmorStand getArmorStand(@Nonnull Block pedestal, boolean createIfNoneExists) {
Optional<Item> entity = getPlacedItem(pedestal);

if (entity.isPresent() && entity.get().getVehicle() instanceof ArmorStand armorStand) {
return armorStand;
}

Location l = pedestal.getLocation().add(0.5, 1.2, 0.5);
for (Entity n : l.getWorld().getNearbyEntities(l, 0.5, 0.5, 0.5, this::testArmorStand)) {
if (n instanceof ArmorStand armorStand) {
return armorStand;
}
}

return createIfNoneExists ? ArmorStandUtils.spawnArmorStand(l) : null;
}

private boolean testItem(@Nullable Entity n) {
if (n instanceof Item item && n.isValid()) {
Expand All @@ -103,17 +129,24 @@ private boolean testItem(@Nullable Entity n) {
return false;
}
}

private boolean testArmorStand(@Nullable Entity n) {
if (n instanceof ArmorStand armorStand && armorStand.isValid()) {
String customName = armorStand.getCustomName();
JustAHuman-xD marked this conversation as resolved.
Show resolved Hide resolved
return customName != null && customName.startsWith(ITEM_PREFIX);
} else {
return false;
}
}

public @Nonnull ItemStack getOriginalItemStack(@Nonnull Item item) {
ItemStack stack = item.getItemStack().clone();
ItemMeta im = stack.getItemMeta();
String customName = item.getCustomName();
im.setDisplayName(null);
stack.setItemMeta(im);

if (customName.equals(ItemUtils.getItemName(new ItemStack(stack.getType())))) {
ItemMeta im = stack.getItemMeta();
im.setDisplayName(null);
stack.setItemMeta(im);
} else {
ItemMeta im = stack.getItemMeta();
if (customName == null || !customName.equals(ItemUtils.getItemName(stack))) {
im.setDisplayName(customName);
stack.setItemMeta(im);
}
Expand All @@ -123,7 +156,8 @@ private boolean testItem(@Nullable Entity n) {

public void placeItem(@Nonnull Player p, @Nonnull Block b) {
ItemStack hand = p.getInventory().getItemInMainHand();
ItemStack displayItem = new CustomItemStack(hand, ITEM_PREFIX + System.nanoTime());
String displayName = ITEM_PREFIX + System.nanoTime();
ItemStack displayItem = new CustomItemStack(hand, displayName);
JustAHuman-xD marked this conversation as resolved.
Show resolved Hide resolved
displayItem.setAmount(1);

// Get the display name of the original Item in the Player's hand
Expand All @@ -136,9 +170,14 @@ public void placeItem(@Nonnull Player p, @Nonnull Block b) {
Item entity = SlimefunUtils.spawnItem(b.getLocation().add(0.5, 1.2, 0.5), displayItem, ItemSpawnReason.ANCIENT_PEDESTAL_PLACE_ITEM);

if (entity != null) {
ArmorStand armorStand = getArmorStand(b, true);
entity.setInvulnerable(true);
entity.setUnlimitedLifetime(true);
entity.setVelocity(new Vector(0, 0.1, 0));
entity.setCustomNameVisible(true);
entity.setCustomName(nametag);
armorStand.setCustomName(displayName);
armorStand.addPassenger(entity);
SlimefunUtils.markAsNoPickup(entity, "altar_item");
p.playSound(b.getLocation(), Sound.ENTITY_ITEM_PICKUP, 0.3F, 0.3F);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import org.bukkit.block.Block;
import org.bukkit.entity.ArmorStand;
import org.bukkit.entity.Entity;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.Player;
import org.bukkit.event.block.BlockPlaceEvent;
import org.bukkit.inventory.ItemStack;
Expand All @@ -26,6 +25,7 @@
import io.github.thebusybiscuit.slimefun4.core.services.holograms.HologramsService;
import io.github.thebusybiscuit.slimefun4.implementation.Slimefun;
import io.github.thebusybiscuit.slimefun4.implementation.handlers.SimpleBlockBreakHandler;
import io.github.thebusybiscuit.slimefun4.utils.ArmorStandUtils;
import io.github.thebusybiscuit.slimefun4.utils.ChatUtils;
import io.github.thebusybiscuit.slimefun4.utils.NumberUtils;

Expand Down Expand Up @@ -152,22 +152,8 @@ private static ArmorStand getArmorStand(@Nonnull Block projector, boolean create
if (!createIfNoneExists) {
return null;
}

ArmorStand hologram = spawnArmorStand(l);
hologram.setCustomName(nametag);
return hologram;
}

private static @Nonnull ArmorStand spawnArmorStand(@Nonnull Location l) {
ArmorStand armorStand = (ArmorStand) l.getWorld().spawnEntity(l, EntityType.ARMOR_STAND);
armorStand.setVisible(false);
armorStand.setSilent(true);
armorStand.setMarker(true);
armorStand.setGravity(false);
armorStand.setBasePlate(false);
armorStand.setCustomNameVisible(true);
armorStand.setRemoveWhenFarAway(false);
return armorStand;

return ArmorStandUtils.spawnArmorStand(l, nametag);
}

private static void killArmorStand(@Nonnull Block b) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package io.github.thebusybiscuit.slimefun4.utils;

import javax.annotation.Nonnull;

import org.bukkit.Location;
import org.bukkit.entity.ArmorStand;

import io.github.thebusybiscuit.slimefun4.core.services.holograms.HologramsService;
import io.github.thebusybiscuit.slimefun4.implementation.items.altar.AncientPedestal;
import io.github.thebusybiscuit.slimefun4.implementation.items.blocks.HologramProjector;

/**
* This class holds utilities for {@link ArmorStand}, useful for classes
* dealing with {@link ArmorStand}s that are not from {@link HologramsService}
*
* @see HologramProjector
* @see AncientPedestal
*
* @author JustAHuman
*/
public class ArmorStandUtils {

/**
JustAHuman-xD marked this conversation as resolved.
Show resolved Hide resolved
* Spawns an {@link ArmorStand} at the given {@link Location} with the given custom name
* <br>
* Set Properties: Invisible, Silent, Marker, No-Gravity, No Base Plate, Don't Remove When Far Away
*
* @param location The {@link Location} to spawn the {@link ArmorStand}
* @param customName The {@link String} custom name the {@link ArmorStand} should display
*
* @return The spawned {@link ArmorStand}
*/
public static @Nonnull ArmorStand spawnArmorStand(@Nonnull Location location, @Nonnull String customName) {
ArmorStand armorStand = spawnArmorStand(location);
armorStand.setCustomName(customName);
armorStand.setCustomNameVisible(true);
return armorStand;
}

/**
* Spawns an {@link ArmorStand} at the given {@link Location}
* <br>
* Set Properties: Invisible, Silent, Marker, No-Gravity, No Base Plate, Don't Remove When Far Away
*
* @param location The {@link Location} to spawn the {@link ArmorStand}
*
* @return The spawned {@link ArmorStand}
*/
public static @Nonnull ArmorStand spawnArmorStand(@Nonnull Location location) {
return location.getWorld().spawn(location, ArmorStand.class, armorStand -> {
armorStand.setVisible(false);
armorStand.setSilent(true);
armorStand.setMarker(true);
armorStand.setGravity(false);
armorStand.setBasePlate(false);
armorStand.setRemoveWhenFarAway(false);
});
}
}