VillagerSummoning/Placing & automatic config updating

This commit is contained in:
Martin Olischläger 2023-05-21 15:59:33 +02:00
parent 4bc7bac6ba
commit fdc98d0c76
8 changed files with 174 additions and 5 deletions

View File

@ -1,17 +1,29 @@
package eu.mhsl.craftattack.spawn;
import eu.mhsl.craftattack.spawn.command.SpawnWorldMuseumVillagerCommand;
import eu.mhsl.craftattack.spawn.listener.PlayerEntityInteractListener;
import eu.mhsl.craftattack.spawn.util.VillagerSpawner;
import org.bukkit.Bukkit;
import org.bukkit.plugin.java.JavaPlugin;
import java.util.Objects;
public final class Main extends JavaPlugin {
@Override
public void onEnable() {
// Plugin startup logic
// register Commands
Objects.requireNonNull(this.getCommand("worldMuseum-villager")).setExecutor(new SpawnWorldMuseumVillagerCommand());
// register listeners
Bukkit.getPluginManager().registerEvents(new PlayerEntityInteractListener(), this);
// Load config & spawn Villager
saveDefaultConfig();
VillagerSpawner.spawnVillager();
}
@Override
public void onDisable() {
// Plugin shutdown logic
VillagerSpawner.killVillager();
}
}

View File

@ -0,0 +1,41 @@
package eu.mhsl.craftattack.spawn.command;
import eu.mhsl.craftattack.spawn.util.ConfigUtil;
import eu.mhsl.craftattack.spawn.util.VillagerSpawner;
import org.bukkit.Location;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.*;
import org.jetbrains.annotations.NotNull;
import java.util.ArrayList;
import java.util.List;
public class SpawnWorldMuseumVillagerCommand implements CommandExecutor {
@Override
public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, @NotNull String[] args) {
if (!(sender instanceof Player player)) return false;
Location playerLocation = player.getLocation();
double x = playerLocation.getX();
double y = playerLocation.getY();
double z = playerLocation.getZ();
double yaw = playerLocation.getYaw();
double pitch = playerLocation.getPitch();
List<Double> cordList = new ArrayList<>() {
{
add(x);
add(y);
add(z);
add(yaw);
add(pitch);
}
};
ConfigUtil.getConfigUtil().getConfig().set("villagerLocation", cordList);
ConfigUtil.getConfigUtil().save();
VillagerSpawner.killVillager();
VillagerSpawner.spawnVillager();
return false;
}
}

View File

@ -0,0 +1,15 @@
package eu.mhsl.craftattack.spawn.listener;
import eu.mhsl.craftattack.spawn.util.VillagerSpawner;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerInteractAtEntityEvent;
public class PlayerEntityInteractListener implements Listener {
@EventHandler
public void onInteract(PlayerInteractAtEntityEvent event) {
if (!event.getRightClicked().equals(VillagerSpawner.getStaticVillager())) return;
event.setCancelled(true);
System.out.println("test");
}
}

View File

@ -0,0 +1,51 @@
package eu.mhsl.craftattack.spawn.util;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import java.io.File;
import java.util.List;
import java.util.Objects;
public class ConfigUtil {
private static ConfigUtil instance;
private final File file;
private final FileConfiguration config;
public static ConfigUtil getConfigUtil() {
if (instance != null) return instance;
instance = new ConfigUtil("config.yml");
return instance;
}
private ConfigUtil(String path) {
this.file = new File(Objects.requireNonNull(Bukkit.getPluginManager().getPlugin("spawn")).getDataFolder().getAbsolutePath() + "/" + path);
this.config = YamlConfiguration.loadConfiguration(this.file);
}
public boolean save() {
try {
this.config.save(this.file);
return true;
}catch (Exception e) {
e.printStackTrace();
return false;
}
}
public FileConfiguration getConfig() {
return this.config;
}
public Location getVillagerLocation() {
List<Double> cordsList = this.config.getDoubleList("villagerLocation");
World world = Bukkit.getWorld("world");
double x = cordsList.get(0);
double y = cordsList.get(1);
double z = cordsList.get(2);
double yaw = cordsList.get(3);
double pitch = cordsList.get(4);
Location location = new Location(world, x, y, z);
location.setYaw((float) yaw);
location.setPitch((float) pitch);
return location;
}
}

View File

@ -0,0 +1,34 @@
package eu.mhsl.craftattack.spawn.util;
import org.bukkit.Location;
import org.bukkit.entity.Entity;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.Villager;
public class VillagerSpawner {
private static Villager staticVillager;
public static void spawnVillager() {
ConfigUtil config = ConfigUtil.getConfigUtil();
Location location = config.getVillagerLocation();
assert EntityType.VILLAGER.getEntityClass() != null;
Entity entity = location.getWorld().spawn(location, EntityType.VILLAGER.getEntityClass());
Villager villager = (Villager) entity;
villager.setAI(false);
villager.setInvulnerable(true);
villager.setSilent(true);
villager.setCollidable(false);
villager.setRemoveWhenFarAway(false);
villager.setProfession(Villager.Profession.LIBRARIAN);
staticVillager = villager;
}
public static void killVillager() {
staticVillager.remove();
}
public static Villager getStaticVillager() {
return staticVillager;
}
}

View File

@ -0,0 +1,6 @@
villagerLocation:
- 0 #x
- 0 #y
- 0 #z
- 0 #yaw
- 0 #pitch

View File

@ -1,5 +1,7 @@
name: spawn
version: '${version}'
version: 1.0
main: eu.mhsl.craftattack.spawn.Main
api-version: 1.19
load: STARTUP
commands:
spawnWorldMuseumVillager:
description: Spawns Worldmuseum-villager

View File

@ -0,0 +1,8 @@
name: spawn
version: 1.0
main: eu.mhsl.craftattack.spawn.Main
api-version: 1.19
commands:
worldMuseum-villager:
description: updates Worldmuseum-villager to current player location
usage: /worldMuseum-villager