ZNPCs Hook. FC but needs testing

This commit is contained in:
tastybento 2024-12-25 08:23:35 -08:00
commit ce84d66b91
8 changed files with 105 additions and 143 deletions

View File

@ -3,6 +3,7 @@ on:
push:
branches:
- develop
- master
pull_request:
types: [opened, synchronize, reopened]
jobs:

View File

@ -21,8 +21,8 @@ jobs:
cache: maven
# This step will take the version tag from the release and replace it in `pom.xml` before building.
- name: Set version from release tag
run: mvn -B versions:set -DnewVersion=${{ github.event.release.tag_name }} -DgenerateBackupPoms=false
#- name: Set version from release tag
# run: mvn -B versions:set -DnewVersion=${{ github.event.release.tag_name }} -DgenerateBackupPoms=false
- name: Build and package with Maven
run: mvn -B clean package --file pom.xml

View File

@ -419,11 +419,6 @@
<version>2.0.0-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>lol.pyr</groupId>
<artifactId>ZNPCsPlus</artifactId>
<version>1.0.7</version>
</dependency>
</dependencies>
<build>
@ -475,8 +470,6 @@
<version>3.5.2</version>
<!--suppress MavenModelInspection -->
<configuration>
<parallel>classes</parallel>
<threadCount>8</threadCount>
<argLine>
${argLine}
--add-opens java.base/java.lang=ALL-UNNAMED

View File

@ -19,7 +19,6 @@ import org.eclipse.jdt.annotation.Nullable;
import world.bentobox.bentobox.api.configuration.Config;
import world.bentobox.bentobox.api.events.BentoBoxReadyEvent;
import world.bentobox.bentobox.api.hooks.Hook;
import world.bentobox.bentobox.api.localization.TextVariables;
import world.bentobox.bentobox.api.user.Notifier;
import world.bentobox.bentobox.api.user.User;
@ -33,6 +32,7 @@ import world.bentobox.bentobox.hooks.MyWorldsHook;
import world.bentobox.bentobox.hooks.MythicMobsHook;
import world.bentobox.bentobox.hooks.SlimefunHook;
import world.bentobox.bentobox.hooks.VaultHook;
import world.bentobox.bentobox.hooks.ZNPCsPlusHook;
import world.bentobox.bentobox.hooks.placeholders.PlaceholderAPIHook;
import world.bentobox.bentobox.listeners.BannedCommands;
import world.bentobox.bentobox.listeners.BlockEndDragon;
@ -196,6 +196,8 @@ public class BentoBox extends JavaPlugin implements Listener {
// FancyNpcs
hooksManager.registerHook(new FancyNpcsHook());
// ZNPCsPlus
hooksManager.registerHook(new ZNPCsPlusHook());
// MythicMobs
hooksManager.registerHook(new MythicMobsHook());

View File

@ -167,8 +167,8 @@ public class Database<T> {
}
/**
* Load all objects async
* @return CompletableFuture<List<T>>
* Load all objects asynchronously.
* @return {@code CompletableFuture<List<T>>}
*/
public @NonNull CompletableFuture<List<T>> loadObjectsASync() {
return handler.loadObjectsASync();

View File

@ -1,130 +0,0 @@
package world.bentobox.bentobox.hooks;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.UUID;
import java.util.stream.Collectors;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.configuration.InvalidConfigurationException;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.util.Vector;
import org.eclipse.jdt.annotation.Nullable;
import de.oliver.fancynpcs.api.FancyNpcsPlugin;
import lol.pyr.znpcsplus.ZNPCsPlus;
import lol.pyr.znpcsplus.api.entity.EntityProperty;
import lol.pyr.znpcsplus.api.interaction.InteractionAction;
import lol.pyr.znpcsplus.api.npc.Npc;
import lol.pyr.znpcsplus.api.npc.NpcType;
import world.bentobox.bentobox.BentoBox;
import world.bentobox.bentobox.api.hooks.Hook;
import world.bentobox.bentobox.blueprints.dataobjects.BlueprintEntity;
/**
* Provides copy and pasting of ZNPCS Plus in blueprints https://github.com/Pyrbu/ZNPCsPlus
*
* @author tastybento
* @since 3.2.0
*/
public class ZNPCSPlusHook extends Hook {
private ZNPCsPlus plugin;
public ZNPCSPlusHook() {
super("ZNPCsPlus", Material.PLAYER_HEAD);
}
public String serializeNPC(Npc npc, Vector origin) {
if (npc == null) {
throw new IllegalArgumentException("NPC cannot be null.");
}
YamlConfiguration config = new YamlConfiguration();
NpcType type = npc.getType();
for (EntityProperty<?> property : npc.getAppliedProperties())
try {
PropertySerializer<?> serializer = propertyRegistry
.getSerializer(((EntityPropertyImpl<?>) property).getType());
if (serializer == null) {
BentoBox.getInstance().logWarning("Unknown serializer for property '" + property.getName()
+ "' for npc '" + npc.getUuid() + "'. skipping ...");
continue;
}
config.set("properties." + property.getName(), serializer.UNSAFE_serialize(npc.getProperty(property)));
} catch (Exception exception) {
BentoBox.getInstance().logWarning(
"Failed to serialize property " + property.getName() + " for npc with id " + npc.getUuid());
exception.printStackTrace();
}
lol.pyr.znpcsplus.api.hologram.Hologram hologram = npc.getHologram();
if (hologram.getRefreshDelay() != -1)
config.set("hologram.refresh-delay", hologram.getRefreshDelay());
List<String> lines = new ArrayList<>(npc.getHologram().lineCount());
for (int i = 0; i < npc.getHologram().lineCount(); i++) {
lines.add(hologram.getLine(i));
}
config.set("hologram.lines", lines);
config.set("actions", npc.getActions().stream().map(InteractionAction::toString).filter(Objects::nonNull)
.collect(Collectors.toList()));
return config.saveToString();
}
public boolean spawnNpc(String yaml, Location pos) throws InvalidConfigurationException {
YamlConfiguration npcConfig = new YamlConfiguration();
npcConfig.loadFromString(yaml);
String name = UUID.randomUUID().toString(); // Create a unique name
UUID creator = UUID.randomUUID(); // Random creator
return true;
}
@Override
public boolean hook() {
boolean hooked = this.isPluginAvailable();
if (!hooked) {
BentoBox.getInstance().logError("Could not hook into FancyNpcs");
}
return hooked; // The hook process shouldn't fail
}
@Override
public String getFailureCause() {
return null; // The hook process shouldn't fail
}
public Map<? extends Vector, ? extends List<BlueprintEntity>> getNpcsInArea(World world, List<Vector> vectorsToCopy,
@Nullable Vector origin) {
Map<Vector, List<BlueprintEntity>> bpEntities = new HashMap<>();
for (Npc npc : FancyNpcsPlugin.get().getNpcManager().getAllNpcs()) {
Location npcLocation = npc.getData().getLocation();
Vector spot = new Vector(npcLocation.getBlockX(), npcLocation.getBlockY(), npcLocation.getBlockZ());
if (npcLocation.getWorld().equals(world) && vectorsToCopy.contains(spot)) {
BlueprintEntity cit = new BlueprintEntity();
cit.setType(npc.getData().getType());
cit.setNpc(this.serializeNPC(npc, origin));
// Retrieve or create the list, then add the entity
List<BlueprintEntity> entities = bpEntities.getOrDefault(spot, new ArrayList<>());
entities.add(cit);
// Create position
Vector origin2 = origin == null ? new Vector(0, 0, 0) : origin;
int x = spot.getBlockX() - origin2.getBlockX();
int y = spot.getBlockY() - origin2.getBlockY();
int z = spot.getBlockZ() - origin2.getBlockZ();
Vector pos = new Vector(x, y, z);
// Store
bpEntities.put(pos, entities); // Update the map
}
}
return bpEntities;
}
}

View File

@ -0,0 +1,97 @@
package world.bentobox.bentobox.hooks;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.configuration.InvalidConfigurationException;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.util.Vector;
import org.eclipse.jdt.annotation.Nullable;
import lol.pyr.znpcsplus.api.NpcApiProvider;
import lol.pyr.znpcsplus.api.npc.NpcEntry;
import lol.pyr.znpcsplus.util.NpcLocation;
import world.bentobox.bentobox.BentoBox;
import world.bentobox.bentobox.api.hooks.Hook;
import world.bentobox.bentobox.blueprints.dataobjects.BlueprintEntity;
/**
* Provides copy and pasting of ZNPCS Plus in blueprints https://github.com/Pyrbu/ZNPCsPlus
*
* @author tastybento
* @since 3.2.0
*/
public class ZNPCsPlusHook extends Hook {
public ZNPCsPlusHook() {
super("ZNPCsPlus", Material.PLAYER_HEAD);
}
public String serializeNPC(NpcEntry entry, Vector origin) {
String result = NpcApiProvider.get().getNpcSerializerRegistry().getSerializer(YamlConfiguration.class)
.serialize(entry)
.saveToString();
BentoBox.getInstance().logDebug(result);
return result;
}
public boolean spawnNpc(String yaml, Location pos) throws InvalidConfigurationException {
YamlConfiguration yaml2 = new YamlConfiguration();
yaml2.loadFromString(yaml);
NpcEntry entry = NpcApiProvider.get().getNpcSerializerRegistry().getSerializer(YamlConfiguration.class)
.deserialize(yaml2);
NpcLocation loc = new NpcLocation(pos);
entry.getNpc().setLocation(loc);
NpcApiProvider.get().getNpcRegistry().register(entry);
return true;
}
@Override
public boolean hook() {
boolean hooked = this.isPluginAvailable();
// Check version
String version = this.getPlugin().getDescription().getVersion();
BentoBox.getInstance().logDebug("ZNPCsPlus version = " + version);
if (!hooked) {
BentoBox.getInstance().logError("Could not hook into FancyNpcs");
}
return hooked; // The hook process shouldn't fail
}
@Override
public String getFailureCause() {
return null; // The hook process shouldn't fail
}
public Map<? extends Vector, ? extends List<BlueprintEntity>> getNpcsInArea(World world, List<Vector> vectorsToCopy,
@Nullable Vector origin) {
Map<Vector, List<BlueprintEntity>> bpEntities = new HashMap<>();
for (NpcEntry npc : NpcApiProvider.get().getNpcRegistry().getAll()) {
NpcLocation npcLocation = npc.getNpc().getLocation();
Vector spot = new Vector(npcLocation.getBlockX(), npcLocation.getBlockY(), npcLocation.getBlockZ());
if (npc.getNpc().getWorld().equals(world) && vectorsToCopy.contains(spot)) {
BlueprintEntity cit = new BlueprintEntity();
//cit.setType(npc.getNpc().getType());
cit.setNpc(this.serializeNPC(npc, origin));
// Retrieve or create the list, then add the entity
List<BlueprintEntity> entities = bpEntities.getOrDefault(spot, new ArrayList<>());
entities.add(cit);
// Create position
Vector origin2 = origin == null ? new Vector(0, 0, 0) : origin;
int x = spot.getBlockX() - origin2.getBlockX();
int y = spot.getBlockY() - origin2.getBlockY();
int z = spot.getBlockZ() - origin2.getBlockZ();
Vector pos = new Vector(x, y, z);
// Store
bpEntities.put(pos, entities); // Update the map
}
}
return bpEntities;
}
}

View File

@ -60,7 +60,6 @@ public class IslandHomesPanel extends AbstractPanel
*
* @param command CompositeCommand
* @param user User who opens panel
* @param islandMap map of island names and IslandInfo
*/
private IslandHomesPanel(@NonNull CompositeCommand command, @NonNull User user)
{