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

NPC Draft #26

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
24 changes: 24 additions & 0 deletions api/src/main/java/de/minebench/tresor/services/npc/NPC.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package de.minebench.tresor.services.npc;

import de.minebench.tresor.services.npc.data.Interaction;
import de.minebench.tresor.services.npc.data.NPCMetadata;
import java.util.UUID;
import java.util.function.Consumer;
import org.bukkit.Location;

public interface NPC {

UUID getUniqueId();

boolean isPersistent();

NPCMetadata getMetadata();
void updateMetadata(NPCMetadata metadata);

Location getLocation();
void teleport(Location location);

void onInteract(Consumer<Interaction> action);

void remove();
}
24 changes: 24 additions & 0 deletions api/src/main/java/de/minebench/tresor/services/npc/NPCs.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package de.minebench.tresor.services.npc;

import de.minebench.tresor.services.TresorServiceProvider;
import java.util.UUID;
import org.bukkit.Location;

public interface NPCs extends TresorServiceProvider {

default NPC createNPC(String name, Location location) {
return createNPC(name, location, true);
}

NPC createNPC(String name, Location location, boolean persistent);
NPC getNPC(UUID uniqueId);

void removeNPC(UUID uniqueId);

boolean supports(Feature feature);

enum Feature {
PERSISTENT
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package de.minebench.tresor.services.npc.data;

import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;

public class Interaction {

private final Player player;
private final Type type;
private final ItemStack heldItem;

public Interaction(Player player, Type type, ItemStack heldItem) {
this.player = player;
this.type = type;
this.heldItem = heldItem;
}

public Player getPlayer() {
return player;
}

public Type getType() {
return type;
}

public ItemStack getHeldItem() {
return heldItem;
}

public enum Type {
LEFT_CLICK,
RIGHT_CLICK
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package de.minebench.tresor.services.npc.data;

public class NPCMetadata {

private String displayName;
private NPCSkinData skinData;

public NPCMetadata(String displayName, NPCSkinData skinData) {
this.displayName = displayName;
this.skinData = skinData;
}

public String getDisplayName() {
return displayName;
}

public NPCSkinData getSkinData() {
return skinData;
}

public void setDisplayName(String displayName) {
this.displayName = displayName;
}

public void setSkinData(NPCSkinData skinData) {
this.skinData = skinData;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package de.minebench.tresor.services.npc.data;

public class NPCSkinData {

private final String texture;
private final String signature;

public NPCSkinData(String texture, String signature) {
this.texture = texture;
this.signature = signature;
}

public String getTexture() {
return texture;
}

public String getSignature() {
return signature;
}
}
40 changes: 40 additions & 0 deletions providers/citizens/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>de.minebench.tresor.providers</groupId>
<artifactId>citizens</artifactId>

<parent>
<groupId>de.minebench.tresor</groupId>
<artifactId>tresor-providers</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>

<repositories>
<repository>
<id>citizens-repo</id>
<url>https://maven.citizensnpcs.co/repo</url>
</repository>
</repositories>

<dependencies>
<dependency>
<groupId>net.citizensnpcs</groupId>
<artifactId>citizens-main</artifactId>
<version>2.0.33-SNAPSHOT</version>
<type>jar</type>
<scope>provided</scope>
<exclusions>
<exclusion>
<groupId>*</groupId>
<artifactId>*</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>


</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
package de.minebench.tresor.providers.citizens;

import de.minebench.tresor.Provider;
import de.minebench.tresor.providers.citizens.listener.CitizensNPCListener;
import de.minebench.tresor.services.npc.NPC;
import de.minebench.tresor.services.npc.NPCs;
import java.util.UUID;
import net.citizensnpcs.api.CitizensPlugin;
import net.citizensnpcs.api.npc.MemoryNPCDataStore;
import net.citizensnpcs.api.npc.NPCRegistry;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.entity.EntityType;

public class CitizensNPCs extends Provider<NPCs, CitizensPlugin> implements NPCs {

private NPCRegistry persistentRegistry;
private NPCRegistry temporaryRegistry;

private CitizensPlugin hooked;

public CitizensNPCs() {
super(NPCs.class);
}

@Override
public void register() {
super.register();
Bukkit.getPluginManager().registerEvents(new CitizensNPCListener(this), getHooked());
}

@Override
public void unregister() {
super.unregister();
// TODO
}

@Override
public boolean isEnabled() {
return getHooked() != null && getHooked().isEnabled();
}

@Override
public NPC createNPC(String name, Location location, boolean persistent) {
NPCRegistry registry = persistent ? persistentRegistry : temporaryRegistry;
net.citizensnpcs.api.npc.NPC rawNPC = registry.createNPC(EntityType.PLAYER, name);
rawNPC.spawn(location);

return new WrappedNPC(rawNPC, persistent);
}

@Override
public NPC getNPC(UUID uniqueId) {
net.citizensnpcs.api.npc.NPC rawNPC = persistentRegistry.getByUniqueIdGlobal(uniqueId);
boolean isPersistent = true;

if (rawNPC == null) {
rawNPC = temporaryRegistry.getByUniqueIdGlobal(uniqueId);
isPersistent = false;
}

if (rawNPC == null) {
return null;
}

return new WrappedNPC(rawNPC, isPersistent);
}

@Override
public void removeNPC(UUID uniqueId) {
NPC npc = getNPC(uniqueId);

if (npc != null) {
npc.remove();
}
}

@Override
public boolean supports(Feature feature) {
return feature == Feature.PERSISTENT;
}

@Override
public CitizensPlugin getHooked() {
if(hooked == null) {
hooked = (CitizensPlugin) Bukkit.getPluginManager().getPlugin(getName());
ensureRegistries();
}

return hooked;
}

@Override
public String getName() {
return "Citizens";
}

private void ensureRegistries() {
if (persistentRegistry == null) {
persistentRegistry = hooked.getNPCRegistry();
}

if (temporaryRegistry == null) {
temporaryRegistry = hooked.createAnonymousNPCRegistry(new MemoryNPCDataStore());
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package de.minebench.tresor.providers.citizens;

import de.minebench.tresor.services.npc.NPC;
import de.minebench.tresor.services.npc.data.Interaction;
import de.minebench.tresor.services.npc.data.NPCMetadata;
import de.minebench.tresor.services.npc.data.NPCSkinData;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import java.util.function.Consumer;
import net.citizensnpcs.trait.SkinTrait;
import org.bukkit.Location;
import org.bukkit.event.player.PlayerTeleportEvent.TeleportCause;

public class WrappedNPC implements NPC {

private final List<Consumer<Interaction>> interactActions = new ArrayList<>();
private final net.citizensnpcs.api.npc.NPC npc;
private final boolean persistent;

public WrappedNPC(net.citizensnpcs.api.npc.NPC npc, boolean persistent) {
this.npc = npc;
this.persistent = persistent;
}

@Override
public UUID getUniqueId() {
return npc.getUniqueId();
}

@Override
public boolean isPersistent() {
return persistent;
}

@Override
public NPCMetadata getMetadata() {
String displayName = npc.getName();
SkinTrait skinTrait = npc.getTraitNullable(SkinTrait.class);

if (skinTrait == null) {
return new NPCMetadata(displayName, null);
}

NPCSkinData skinData = new NPCSkinData(skinTrait.getTexture(), skinTrait.getSignature());
return new NPCMetadata(displayName, skinData);
}

@Override
public void updateMetadata(NPCMetadata metadata) {
npc.setName(metadata.getDisplayName());

NPCSkinData skinData = metadata.getSkinData();

if (skinData == null) {
return;
}

SkinTrait skinTrait = npc.getOrAddTrait(SkinTrait.class);
skinTrait.setTexture(skinData.getTexture(), skinData.getSignature());
}

@Override
public Location getLocation() {
return npc.getStoredLocation();
}

@Override
public void teleport(Location location) {
npc.teleport(location, TeleportCause.PLUGIN);
}

@Override
public void onInteract(Consumer<Interaction> action) {
interactActions.add(action);
}

@Override
public void remove() {
npc.destroy();
}

public void handleInteraction(Interaction interaction) { // I don't think this should be exposed
for (Consumer<Interaction> action : interactActions) {
action.accept(interaction);
}
}
}
Loading