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

Fancynpc #2570

Merged
merged 4 commits into from
Dec 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@
<!-- Do not change unless you want different name for local builds. -->
<build.number>-LOCAL</build.number>
<!-- This allows to change between versions. -->
<build.version>3.0.1</build.version>
<build.version>3.1.0</build.version>
<sonar.organization>bentobox-world</sonar.organization>
<sonar.host.url>https://sonarcloud.io</sonar.host.url>
<server.jars>${project.basedir}/lib</server.jars>
Expand Down Expand Up @@ -192,6 +192,12 @@
<id>clojars</id>
<url>https://repo.clojars.org/</url>
</repository>
<!-- FancyNPC -->
<repository>
<id>fancyplugins-releases</id>
<name>FancyPlugins Repository</name>
<url>https://repo.fancyplugins.de/releases</url>
</repository>
</repositories>

<dependencies>
Expand Down Expand Up @@ -393,6 +399,13 @@
<version>1.1.13</version>
<scope>compile</scope>
</dependency>
<!-- FancyNPCs -->
<dependency>
<groupId>de.oliver</groupId>
<artifactId>FancyNpcs</artifactId>
<version>2.4.0</version>
<scope>provided</scope>
</dependency>
</dependencies>

<build>
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/world/bentobox/bentobox/BentoBox.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,13 @@

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;
import world.bentobox.bentobox.commands.BentoBoxCommand;
import world.bentobox.bentobox.database.DatabaseSetup;
import world.bentobox.bentobox.hooks.FancyNpcsHook;
import world.bentobox.bentobox.hooks.ItemsAdderHook;
import world.bentobox.bentobox.hooks.MultipaperHook;
import world.bentobox.bentobox.hooks.MultiverseCoreHook;
Expand Down Expand Up @@ -192,6 +194,9 @@ private void completeSetup(long loadTime) {

hooksManager.registerHook(new VaultHook());

// FancyNpcs
hooksManager.registerHook(new FancyNpcsHook());

// MythicMobs
hooksManager.registerHook(new MythicMobsHook());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
Expand All @@ -23,8 +22,8 @@
import org.bukkit.entity.AbstractHorse;
import org.bukkit.entity.Ageable;
import org.bukkit.entity.ChestedHorse;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Horse;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Player;
import org.bukkit.entity.Tameable;
import org.bukkit.entity.Villager;
Expand All @@ -44,6 +43,7 @@
import world.bentobox.bentobox.blueprints.dataobjects.BlueprintBlock;
import world.bentobox.bentobox.blueprints.dataobjects.BlueprintCreatureSpawner;
import world.bentobox.bentobox.blueprints.dataobjects.BlueprintEntity;
import world.bentobox.bentobox.hooks.FancyNpcsHook;
import world.bentobox.bentobox.hooks.MythicMobsHook;

/**
Expand All @@ -70,20 +70,22 @@ public class BlueprintClipboard {
private final Map<Vector, BlueprintBlock> bpBlocks = new LinkedHashMap<>();
private final BentoBox plugin = BentoBox.getInstance();
private Optional<MythicMobsHook> mmh;
private Optional<FancyNpcsHook> npc;

/**
* Create a clipboard for blueprint
* @param blueprint - the blueprint to load into the clipboard
*/
public BlueprintClipboard(@NonNull Blueprint blueprint) {
this();
this.blueprint = blueprint;
// MythicMobs
mmh = plugin.getHooks().getHook("MythicMobs").filter(MythicMobsHook.class::isInstance)
.map(MythicMobsHook.class::cast);
}

public BlueprintClipboard() {
// MythicMobs
// Citizens Hook
npc = plugin.getHooks().getHook("FancyNpcs").filter(FancyNpcsHook.class::isInstance)
.map(FancyNpcsHook.class::cast);
// MythicMobs Hook
mmh = plugin.getHooks().getHook("MythicMobs").filter(MythicMobsHook.class::isInstance)
.map(MythicMobsHook.class::cast);
}
Expand Down Expand Up @@ -136,13 +138,20 @@ public boolean copy(User user, boolean copyAir, boolean copyBiome) {

private void copyAsync(World world, User user, List<Vector> vectorsToCopy, int speed, boolean copyAir, boolean copyBiome) {
copying = false;
// FancyNpcs
if (npc.isPresent()) {
// Add all the citizens for the area in one go. This is pretty fast.
bpEntities.putAll(npc.get().getNpcsInArea(world, vectorsToCopy, origin));
}

// Repeating copy task
copyTask = Bukkit.getScheduler().runTaskTimer(plugin, () -> {
if (copying) {
return;
}
copying = true;
vectorsToCopy.stream().skip(index).limit(speed).forEach(v -> {
List<LivingEntity> ents = world.getLivingEntities().stream()
List<Entity> ents = world.getEntities().stream()
.filter(Objects::nonNull)
.filter(e -> !(e instanceof Player))
.filter(e -> new Vector(Math.rint(e.getLocation().getX()),
Expand All @@ -153,6 +162,7 @@ private void copyAsync(World world, User user, List<Vector> vectorsToCopy, int s
count++;
}
});

index += speed;
int percent = (int)(index * 100 / (double)vectorsToCopy.size());
if (percent != lastPercentage && percent % 10 == 0) {
Expand Down Expand Up @@ -189,9 +199,9 @@ protected List<Vector> getVectors(BoundingBox b) {
return r;
}

private boolean copyBlock(Location l, boolean copyAir, boolean copyBiome, Collection<LivingEntity> entities) {
private boolean copyBlock(Location l, boolean copyAir, boolean copyBiome, List<Entity> ents) {
Block block = l.getBlock();
if (!copyAir && block.getType().equals(Material.AIR) && entities.isEmpty()) {
if (!copyAir && block.getType().equals(Material.AIR) && ents.isEmpty()) {
return false;
}
// Create position
Expand All @@ -202,14 +212,14 @@ private boolean copyBlock(Location l, boolean copyAir, boolean copyBiome, Collec
Vector pos = new Vector(x, y, z);

// Set entities
List<BlueprintEntity> bpEnts = setEntities(entities);
List<BlueprintEntity> bpEnts = setEntities(ents);
// Store
if (!bpEnts.isEmpty()) {
bpEntities.put(pos, bpEnts);
}

// Return if this is just air block
if (!copyAir && block.getType().equals(Material.AIR) && !entities.isEmpty()) {
if (!copyAir && block.getType().equals(Material.AIR) && !ents.isEmpty()) {
return true;
}

Expand Down Expand Up @@ -291,9 +301,14 @@ private BlueprintCreatureSpawner getSpawner(CreatureSpawner spawner) {
return cs;
}

private List<BlueprintEntity> setEntities(Collection<LivingEntity> entities) {
/**
* Deals with any entities that are in this block. Technically, this could be more than one, but is usually one.
* @param ents collection of entities
* @return Serialized list of entities
*/
private List<BlueprintEntity> setEntities(List<Entity> ents) {
List<BlueprintEntity> bpEnts = new ArrayList<>();
for (LivingEntity entity: entities) {
for (Entity entity : ents) {
BlueprintEntity bpe = new BlueprintEntity();

bpe.setType(entity.getType());
Expand Down Expand Up @@ -329,6 +344,7 @@ private List<BlueprintEntity> setEntities(Collection<LivingEntity> entities) {
bpe.setStyle(horse.getStyle());
}

// Mythic mob check
mmh.filter(mm -> mm.isMythicMob(entity)).map(mm -> mm.getMythicMob(entity))
.ifPresent(bpe::setMythicMobsRecord);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,12 +124,38 @@ public BlueprintPaster(@NonNull BentoBox plugin, @NonNull Blueprint bp, World wo
location.setY(y);
}

private record Bits(Map<Vector, BlueprintBlock> blocks,
/**
* A record of all the "bits" of the blueprint that need to be pasted
* Consists of blocks, attached blocks, entities, iterators for the blocks and a speed
*/
private record Bits(
/**
* Basic blocks to the pasted (not attached blocks)
*/
Map<Vector, BlueprintBlock> blocks,
/**
* Attached blocks
*/
Map<Vector, BlueprintBlock> attached,
/**
* Entities to be pasted
*/
Map<Vector, List<BlueprintEntity>> entities,
/**
* Basic block pasting iterator
*/
Iterator<Entry<Vector, BlueprintBlock>> it,
/**
* Attached block pasting iterator
*/
Iterator<Entry<Vector, BlueprintBlock>> it2,
/**
* Entity pasting iterator
*/
Iterator<Entry<Vector, List<BlueprintEntity>>> it3,
/**
* Paste speed
*/
int pasteSpeed) {}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@
*/
public class BlueprintEntity {

// Npc storage
@Expose
private String npc;

// MythicMobs storage
public record MythicMobRecord(String type, String displayName, double level, float power, String stance) {
}

Expand Down Expand Up @@ -303,5 +308,38 @@ public void setMythicMobsRecord(MythicMobRecord mmr) {
this.MMStance = mmr.stance();
this.MMpower = mmr.power();
}

/**
* @return the npc
*/
public String getNpc() {
return npc;
}

/**
* @param citizen the citizen to set
*/
public void setNpc(String citizen) {
this.npc = citizen;
}

@Override
public String toString() {
return "BlueprintEntity [" + (npc != null ? "npc=" + npc + ", " : "")
+ (MMtype != null ? "MMtype=" + MMtype + ", " : "")
+ (MMLevel != null ? "MMLevel=" + MMLevel + ", " : "")
+ (MMStance != null ? "MMStance=" + MMStance + ", " : "")
+ (MMpower != null ? "MMpower=" + MMpower + ", " : "") + (color != null ? "color=" + color + ", " : "")
+ (type != null ? "type=" + type + ", " : "")
+ (customName != null ? "customName=" + customName + ", " : "")
+ (tamed != null ? "tamed=" + tamed + ", " : "") + (chest != null ? "chest=" + chest + ", " : "")
+ (adult != null ? "adult=" + adult + ", " : "")
+ (domestication != null ? "domestication=" + domestication + ", " : "")
+ (inventory != null ? "inventory=" + inventory + ", " : "")
+ (style != null ? "style=" + style + ", " : "") + (level != null ? "level=" + level + ", " : "")
+ (profession != null ? "profession=" + profession + ", " : "")
+ (experience != null ? "experience=" + experience + ", " : "")
+ (villagerType != null ? "villagerType=" + villagerType : "") + "]";
}

}
Loading
Loading