Added base event-system
Added Help
This commit is contained in:
@@ -1,6 +1,9 @@
|
||||
package eu.mhsl.craftattack.spawn.util;
|
||||
|
||||
import eu.mhsl.craftattack.spawn.config.ConfigUtil;
|
||||
import eu.mhsl.craftattack.spawn.config.Configuration;
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.configuration.ConfigurationSection;
|
||||
import org.bukkit.entity.EntityType;
|
||||
import org.bukkit.entity.Villager;
|
||||
|
||||
@@ -43,4 +46,42 @@ public class DisplayVillager {
|
||||
villager.setCustomNameVisible(true);
|
||||
return villager;
|
||||
}
|
||||
|
||||
public static class ConfigBound {
|
||||
private final DisplayVillager villager;
|
||||
private final ConfigurationSection config;
|
||||
public ConfigBound(ConfigurationSection configurationSection, Consumer<Villager> villagerCreator) {
|
||||
this.config = configurationSection;
|
||||
|
||||
Location location = ConfigUtil.Position.paseLocation(Objects.requireNonNull(this.config.getConfigurationSection("villagerLocation")));
|
||||
this.villager = new DisplayVillager(
|
||||
UUID.fromString(this.config.getString("uuid", UUID.randomUUID().toString())),
|
||||
location,
|
||||
villager -> {
|
||||
this.config.set("uuid", villager.getUniqueId().toString());
|
||||
Configuration.saveChanges();
|
||||
|
||||
villagerCreator.accept(villager);
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
public void updateLocation(Location location) {
|
||||
ConfigUtil.Position.writeLocation(
|
||||
Objects.requireNonNull(this.config.getConfigurationSection("villagerLocation")),
|
||||
location
|
||||
);
|
||||
Configuration.saveChanges();
|
||||
|
||||
this.villager.getVillager().teleport(location);
|
||||
}
|
||||
|
||||
public Villager getVillager() {
|
||||
return villager.getVillager();
|
||||
}
|
||||
|
||||
public UUID getUniqueId() {
|
||||
return getVillager().getUniqueId();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,23 @@
|
||||
package eu.mhsl.craftattack.spawn.util.commonListeners;
|
||||
|
||||
import org.bukkit.entity.Entity;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.Listener;
|
||||
import org.bukkit.event.inventory.InventoryOpenEvent;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
public class DismissInventoryOpenFromHolder implements Listener {
|
||||
private final UUID inventoryHolder;
|
||||
|
||||
public DismissInventoryOpenFromHolder(UUID inventoryHolder) {
|
||||
this.inventoryHolder = inventoryHolder;
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void onInventoryOpen(InventoryOpenEvent event) {
|
||||
if(event.getInventory().getHolder() instanceof Entity holder) {
|
||||
if(holder.getUniqueId().equals(this.inventoryHolder)) event.setCancelled(true);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
package eu.mhsl.craftattack.spawn.util.commonListeners;
|
||||
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.Listener;
|
||||
import org.bukkit.event.player.PlayerInteractAtEntityEvent;
|
||||
|
||||
import java.util.UUID;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
public class PlayerInteractAtEntityEventListener implements Listener {
|
||||
private final UUID interactableEntityUUID;
|
||||
private final Consumer<PlayerInteractAtEntityEvent> callback;
|
||||
|
||||
public PlayerInteractAtEntityEventListener(UUID interactableEntityUUID, Consumer<PlayerInteractAtEntityEvent> callback) {
|
||||
this.interactableEntityUUID = interactableEntityUUID;
|
||||
this.callback = callback;
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void onInteract(PlayerInteractAtEntityEvent event) {
|
||||
if (!event.getRightClicked().getUniqueId().equals(this.interactableEntityUUID)) return;
|
||||
this.callback.accept(event);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user