mirror of
https://github.com/YouHaveTrouble/Stand-in.git
synced 2026-05-11 22:16:55 +00:00
do not let editing random entities unless they are spawned with Standin. only armor stands are allowed for editing by default
This commit is contained in:
@@ -7,24 +7,45 @@ import me.youhavetrouble.standin.stand.StandinInteractionListener;
|
||||
import org.bukkit.NamespacedKey;
|
||||
import org.bukkit.entity.Entity;
|
||||
import org.bukkit.entity.EntityType;
|
||||
import org.bukkit.permissions.Permission;
|
||||
import org.bukkit.permissions.PermissionDefault;
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
@SuppressWarnings("UnstableApiUsage")
|
||||
public final class StandIn extends JavaPlugin {
|
||||
|
||||
public static final NamespacedKey KEY = new NamespacedKey("stand-in", "stand-in");
|
||||
|
||||
public final Permission editAnyEntityPermission = new Permission("standin.edit-anything", PermissionDefault.FALSE);
|
||||
public final Permission convertAnyEntityPermission = new Permission("standin.convert-anything", PermissionDefault.FALSE);
|
||||
|
||||
private final Map<EntityType, EntityHandler<? extends Entity>> entityHandlers = new HashMap<>();
|
||||
|
||||
@Override
|
||||
public void onEnable() {
|
||||
getServer().getPluginManager().addPermissions(
|
||||
List.of(
|
||||
editAnyEntityPermission,
|
||||
convertAnyEntityPermission
|
||||
)
|
||||
);
|
||||
|
||||
entityHandlers.put(EntityType.ARMOR_STAND, new ArmorStandHandler());
|
||||
entityHandlers.put(EntityType.MANNEQUIN, new MannequinHandler());
|
||||
|
||||
getServer().getPluginManager().registerEvents(new StandinInteractionListener(), this);
|
||||
entityHandlers.keySet().forEach(entityType -> {
|
||||
Permission editPermission = new Permission("standin.edit." + entityType.getKey().value(), PermissionDefault.OP);
|
||||
Permission convertPermission = new Permission("standin.change_type." + entityType.getKey().value(), PermissionDefault.OP);
|
||||
getServer().getPluginManager().addPermission(editPermission);
|
||||
getServer().getPluginManager().addPermission(convertPermission);
|
||||
});
|
||||
|
||||
getServer().getPluginManager().registerEvents(new StandinInteractionListener(this), this);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -149,7 +149,7 @@ public abstract class EntityHandler<E extends Entity> {
|
||||
if (player.getWorld() != entity.getWorld() || player.getLocation().distanceSquared(entity.getLocation()) > range * range) {
|
||||
return false;
|
||||
}
|
||||
String entityTypeName = entity.getType().toString().toLowerCase(Locale.ENGLISH);
|
||||
String entityTypeName = entity.getType().getKey().value();
|
||||
return switch (action) {
|
||||
case EDIT -> player.hasPermission("standin.edit." + entityTypeName);
|
||||
case CHANGE_TYPE -> player.hasPermission("standin.change_type." + entityTypeName);
|
||||
|
||||
@@ -4,6 +4,7 @@ import io.papermc.paper.dialog.Dialog;
|
||||
import io.papermc.paper.event.player.PlayerPickEntityEvent;
|
||||
import me.youhavetrouble.standin.StandIn;
|
||||
import me.youhavetrouble.standin.entity.EntityHandler;
|
||||
import org.bukkit.entity.ArmorStand;
|
||||
import org.bukkit.entity.Entity;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.event.EventHandler;
|
||||
@@ -14,6 +15,12 @@ import org.jetbrains.annotations.Nullable;
|
||||
|
||||
public class StandinInteractionListener implements Listener {
|
||||
|
||||
private final StandIn plugin;
|
||||
|
||||
public StandinInteractionListener(StandIn plugin) {
|
||||
this.plugin = plugin;
|
||||
}
|
||||
|
||||
private <E extends Entity> @Nullable Dialog invokeEditDialog(EntityHandler<E> handler, Player player, Entity clicked) {
|
||||
return handler.editDialog(player, handler.clazz.cast(clicked));
|
||||
}
|
||||
@@ -25,9 +32,11 @@ public class StandinInteractionListener implements Listener {
|
||||
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
|
||||
public void onInteractWithStands(PlayerInteractAtEntityEvent event) {
|
||||
if (!event.getPlayer().isSneaking()) return;
|
||||
EntityHandler<?> handler = StandIn.getPlugin(StandIn.class).getEntityHandler(event.getRightClicked().getType());
|
||||
Entity entity = event.getRightClicked();
|
||||
if (!(entity instanceof ArmorStand) && !EntityHandler.isStandinEntity(entity) && !event.getPlayer().hasPermission(plugin.editAnyEntityPermission)) return;
|
||||
EntityHandler<?> handler = plugin.getEntityHandler(entity.getType());
|
||||
if (handler == null) return;
|
||||
Dialog dialog = invokeEditDialog(handler, event.getPlayer(), event.getRightClicked());
|
||||
Dialog dialog = invokeEditDialog(handler, event.getPlayer(), entity);
|
||||
if (dialog == null) return;
|
||||
event.getPlayer().showDialog(dialog);
|
||||
event.setCancelled(true);
|
||||
@@ -39,9 +48,11 @@ public class StandinInteractionListener implements Listener {
|
||||
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
|
||||
public void onInteractWithStands(PlayerPickEntityEvent event) {
|
||||
if (!event.getPlayer().isSneaking()) return;
|
||||
EntityHandler<?> handler = StandIn.getPlugin(StandIn.class).getEntityHandler(event.getEntity().getType());
|
||||
Entity entity = event.getEntity();
|
||||
if (!(entity instanceof ArmorStand) && !EntityHandler.isStandinEntity(entity) && !event.getPlayer().hasPermission(plugin.convertAnyEntityPermission)) return;
|
||||
EntityHandler<?> handler = plugin.getEntityHandler(entity.getType());
|
||||
if (handler == null) return;
|
||||
Dialog dialog = invokeConvertDialog(handler, event.getPlayer(), event.getEntity());
|
||||
Dialog dialog = invokeConvertDialog(handler, event.getPlayer(), entity);
|
||||
if (dialog == null) return;
|
||||
event.getPlayer().showDialog(dialog);
|
||||
event.setCancelled(true);
|
||||
|
||||
Reference in New Issue
Block a user