Skip to content

Commit

Permalink
add back spigot support, do a bunch of refactoring & fix lower versio…
Browse files Browse the repository at this point in the history
…ns not having adventure
  • Loading branch information
Pyrbu committed Apr 21, 2023
1 parent c7ff0ed commit 983f240
Show file tree
Hide file tree
Showing 28 changed files with 91 additions and 218 deletions.
6 changes: 5 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ plugins {

repositories {
mavenCentral()
maven {
url "https://hub.spigotmc.org/nexus/content/repositories/snapshots/"
}
maven {
url "https://repo.papermc.io/repository/maven-public/"
}
Expand All @@ -18,7 +21,7 @@ repositories {
}

dependencies {
compileOnly "io.papermc.paper:paper-api:1.19.4-R0.1-SNAPSHOT"
compileOnly "org.spigotmc:spigot-api:1.19.4-R0.1-SNAPSHOT"
compileOnly "me.clip:placeholderapi:2.11.1"

//noinspection GradlePackageUpdate
Expand All @@ -31,6 +34,7 @@ dependencies {
implementation "com.google.code.gson:gson:2.10.1"
implementation "org.bstats:bstats-bukkit:3.0.2"
implementation "com.github.robertlit:SpigotResourcesAPI:2.0"
implementation "net.kyori:adventure-platform-bukkit:4.3.0"
}

group "lol.pyr"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@
import io.github.znetworkw.znpcservers.commands.exception.CommandException;
import io.github.znetworkw.znpcservers.commands.exception.CommandExecuteException;
import io.github.znetworkw.znpcservers.commands.exception.CommandPermissionException;
import org.bukkit.Bukkit;
import io.github.znetworkw.znpcservers.reflection.Reflections;
import org.bukkit.ChatColor;
import org.bukkit.command.CommandMap;
import org.bukkit.command.CommandSender;
import org.bukkit.command.defaults.BukkitCommand;

import java.lang.reflect.Method;
import java.util.*;

@SuppressWarnings("deprecation")
public class Command extends BukkitCommand {
private final Map<CommandInformation, CommandInvoker> subCommands;

Expand All @@ -23,7 +23,7 @@ public Command(String name) {
}

private void load() {
Bukkit.getCommandMap().register(getName(), this);
((CommandMap) Reflections.COMMAND_MAP_FIELD.get()).register(getName(), this);
for (Method method : getClass().getMethods()) {
if (method.isAnnotationPresent(CommandInformation.class)) {
CommandInformation cmdInfo = method.getAnnotation(CommandInformation.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,9 @@

public final class FunctionFactory {
public static ImmutableList<NPCFunction> WITHOUT_FUNCTION = ImmutableList.of(new NPCFunction.WithoutFunction("look"), new NPCFunction.WithoutFunctionSelfUpdate("holo"), new NPCFunction.WithoutFunctionSelfUpdate("mirror"));

public static ImmutableList<NPCFunction> WITH_FUNCTION = ImmutableList.of(new GlowFunction());

public static ImmutableList<NPCFunction> ALL = new ImmutableList.Builder<NPCFunction>()
.addAll(WITHOUT_FUNCTION)
.addAll(WITH_FUNCTION)
.build();

public static ImmutableMap<String, NPCFunction> BY_NAME;

static {
BY_NAME = ALL.stream().collect(GuavaCollectors.toImmutableMap(NPCFunction::getName, function -> function));
}
public static ImmutableList<NPCFunction> ALL = new ImmutableList.Builder<NPCFunction>().addAll(WITHOUT_FUNCTION).addAll(WITH_FUNCTION).build();
public static ImmutableMap<String, NPCFunction> BY_NAME = ALL.stream().collect(GuavaCollectors.toImmutableMap(NPCFunction::getName, function -> function));

public static NPCFunction findFunctionForName(String name) {
return BY_NAME.get(name);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,8 @@

public class NPCAction {
private final ActionType actionType;

private final ClickType clickType;

private final String action;

private int delay;

public NPCAction(ActionType actionType, ClickType clickType, String action, int delay) {
Expand Down Expand Up @@ -65,7 +62,6 @@ public String toString() {
.toString();
}

@SuppressWarnings("deprecation")
enum ActionType {
CMD {
public void run(ZUser user, String actionValue) {
Expand Down
13 changes: 0 additions & 13 deletions src/main/java/io/github/znetworkw/znpcservers/npc/NPCModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,31 +9,18 @@
@SuppressWarnings("unused")
public class NPCModel {
private int id;

private double hologramHeight;

private String skin;

private String signature = "";

private String pathName;

private String glowName;

private ConversationModel conversation;

private ZLocation location;

private NPCType npcType;

private List<String> hologramLines;

private List<NPCAction> clickActions;

private Map<ItemSlot, ItemStack> npcEquip;

private Map<String, Boolean> npcFunctions;

private Map<String, String[]> customizationMap;

public NPCModel(int id) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,8 @@
@SuppressWarnings("ALL")
public interface NPCPath {
void initialize(DataInputStream paramDataInputStream) throws IOException;

void write(DataOutputStream paramDataOutputStream) throws IOException;

void start();

PathInitializer getPath(NPC paramNPC);

interface PathInitializer {
Expand Down Expand Up @@ -88,13 +85,9 @@ public DataInputStream getInputStream() throws IOException {

abstract class AbstractTypeWriter implements NPCPath {
private static final ConcurrentMap<String, AbstractTypeWriter> PATH_TYPES = new ConcurrentHashMap<>();

private static final int PATH_DELAY = 1;

private final TypeWriter typeWriter;

private final File file;

private final List<ZLocation> locationList;

public AbstractTypeWriter(TypeWriter typeWriter, File file) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,8 @@
import io.github.znetworkw.znpcservers.utility.Utils;

public class NPCSkin {
private static final String[] EMPTY_ARRAY = new String[0];

private static final int LAYER_INDEX = SkinLayerValues.findLayerByVersion();

private final String texture;

private final String signature;

protected NPCSkin(String... values) {
Expand All @@ -21,7 +17,7 @@ protected NPCSkin(String... values) {
}

public static NPCSkin forValues(String... values) {
return new NPCSkin((values.length > 0) ? values : EMPTY_ARRAY);
return new NPCSkin((values.length > 0) ? values : new String[0]);
}

public static void forName(String skin, SkinFetcherResult skinFetcherResult) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,13 @@ public enum TypeProperty {
}

public static TypeProperty forType(Class<?> primitiveType) {
if (primitiveType == String.class)
return STRING;
if (primitiveType == boolean.class)
return BOOLEAN;
if (primitiveType == int.class)
return INT;
if (primitiveType == double.class)
return DOUBLE;
if (primitiveType == float.class)
return FLOAT;
if (primitiveType == short.class)
return SHORT;
if (primitiveType == long.class)
return LONG;
if (primitiveType == String.class) return STRING;
if (primitiveType == boolean.class) return BOOLEAN;
if (primitiveType == int.class) return INT;
if (primitiveType == double.class) return DOUBLE;
if (primitiveType == float.class) return FLOAT;
if (primitiveType == short.class) return SHORT;
if (primitiveType == long.class) return LONG;
return null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,8 @@

public class Conversation {
private final String name;

private final List<ConversationKey> texts;

private int radius = 5;

private int delay = 10;

public Conversation(String name) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,9 @@

public class ConversationKey {
private static final Splitter SPACE_SPLITTER = Splitter.on(" ");

private final List<String> lines;

private final List<NPCAction> actions;

private int delay = 1;

private String soundName;

public ConversationKey(String line) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,10 @@

public class ConversationProcessor {
private static final Map<UUID, String> RUNNING_CONVERSATIONS = new HashMap<>();

private final NPC npc;

private final ConversationModel conversationModel;

private final Player player;

private int conversationIndex = 0;

private long conversationIndexDelay = System.nanoTime();

public ConversationProcessor(NPC npc, ConversationModel conversationModel, Player player) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import org.bukkit.event.Event;
import org.bukkit.event.HandlerList;

@SuppressWarnings("unused")
public class NPCInteractEvent extends Event {
private static final HandlerList handlerList = new HandlerList();
private final Player player;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

import java.util.concurrent.ThreadLocalRandom;

@SuppressWarnings("deprecation")
public class RGBLine implements LineReplacer {
public String make(String string) {
String rgbString = string;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import io.github.znetworkw.znpcservers.reflection.types.*;
import io.github.znetworkw.znpcservers.utility.Utils;
import io.netty.channel.Channel;
import org.bukkit.Bukkit;
import org.bukkit.inventory.ItemStack;

import java.lang.reflect.Constructor;
Expand Down Expand Up @@ -538,22 +539,26 @@ public final class Reflections {
public static final ReflectionLazyLoader<Object> ADD_PLAYER_FIELD = new FieldReflection(new ReflectionBuilder(ReflectionPackage.PACKET)
.withClassName("PacketPlayOutPlayerInfo$EnumPlayerInfoAction")
.withClassName("ClientboundPlayerInfoUpdatePacket$a")
.withFieldName((Utils.BUKKIT_VERSION > 16) ? "a" : "ADD_PLAYER")).asValueField();
.withFieldName((Utils.BUKKIT_VERSION > 16) ? "a" : "ADD_PLAYER")).staticValueLoader();

public static final ReflectionLazyLoader<Object> UPDATE_LISTED_FIELD = new FieldReflection(new ReflectionBuilder(ReflectionPackage.PACKET)
.withClassName("ClientboundPlayerInfoUpdatePacket$a")
.withFieldName("d")).asValueField();
.withFieldName("d")).staticValueLoader();

public static final ReflectionLazyLoader<Object> REMOVE_PLAYER_FIELD = new FieldReflection(new ReflectionBuilder(ReflectionPackage.PACKET)
.withClassName("PacketPlayOutPlayerInfo$EnumPlayerInfoAction")
.withClassName("ClientboundPlayerInfoUpdatePacket$a")
.withFieldName((Utils.BUKKIT_VERSION > 16) ? "e" : "REMOVE_PLAYER")).asValueField();
.withFieldName((Utils.BUKKIT_VERSION > 16) ? "e" : "REMOVE_PLAYER")).staticValueLoader();

public static final ReflectionLazyLoader<Object> DATA_WATCHER_REGISTER_FIELD = new FieldReflection(new ReflectionBuilder(ReflectionPackage.PACKET)
.withClassName(DATA_WATCHER_REGISTRY)
.withFieldName("a")).asValueField();
.withFieldName("a")).staticValueLoader();

public static final ReflectionLazyLoader<Object> ENUM_TAG_VISIBILITY_NEVER_FIELD = new FieldReflection(new ReflectionBuilder(ReflectionPackage.PACKET)
.withClassName(ENUM_TAG_VISIBILITY)
.withFieldName("b")).asValueField();
.withFieldName("b")).staticValueLoader();

public static final ReflectionLazyLoader<Object> COMMAND_MAP_FIELD = new FieldReflection(new ReflectionBuilder(ReflectionPackage.BUKKIT)
.withClassName("CraftServer")
.withFieldName("commandMap")).valueLoader(Bukkit.getServer());
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,21 +29,27 @@ protected Field load() throws NoSuchFieldException {
return field;
}

public AsValueField asValueField() {
return new AsValueField(this, possibleClassNames);
public FieldValueReflection staticValueLoader() {
return new FieldValueReflection(this, possibleClassNames, null);
}

private static class AsValueField extends ReflectionLazyLoader<Object> {
public FieldValueReflection valueLoader(Object obj) {
return new FieldValueReflection(this, possibleClassNames, obj);
}

private static class FieldValueReflection extends ReflectionLazyLoader<Object> {
private final Object obj;
private final FieldReflection fieldReflection;

public AsValueField(FieldReflection fieldReflection, List<String> className) {
public FieldValueReflection(FieldReflection fieldReflection, List<String> className, Object obj) {
super(className);
this.obj = obj;
this.fieldReflection = fieldReflection;
}

protected Object load() throws IllegalAccessException, NoSuchFieldException {
Field field = this.fieldReflection.load();
return field.get(null);
Field field = this.fieldReflection.get();
return field.get(obj);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

public class SkinFetcher {
private static final ExecutorService SKIN_EXECUTOR_SERVICE = Executors.newCachedThreadPool();

private final SkinFetcherBuilder builder;

public SkinFetcher(SkinFetcherBuilder builder) {
Expand All @@ -31,32 +30,12 @@ public CompletableFuture<JsonObject> doReadSkin(SkinFetcherResult skinFetcherRes
connection.setRequestMethod(this.builder.getAPIServer().getMethod());
if (this.builder.isUrlType()) {
connection.setDoOutput(true);
DataOutputStream outputStream = new DataOutputStream(connection.getOutputStream());
try {
try (DataOutputStream outputStream = new DataOutputStream(connection.getOutputStream())) {
outputStream.writeBytes("url=" + URLEncoder.encode(this.builder.getData(), StandardCharsets.UTF_8));
outputStream.close();
} catch (Throwable throwable) {
try {
outputStream.close();
} catch (Throwable throwable1) {
throwable.addSuppressed(throwable1);
}
throw throwable;
}
}
try {
Reader reader = new InputStreamReader(connection.getInputStream(), StandardCharsets.UTF_8);
try {
completableFuture.complete(JsonParser.parseReader(reader).getAsJsonObject());
reader.close();
} catch (Throwable throwable) {
try {
reader.close();
} catch (Throwable throwable1) {
throwable.addSuppressed(throwable1);
}
throw throwable;
}
try (Reader reader = new InputStreamReader(connection.getInputStream(), StandardCharsets.UTF_8)) {
completableFuture.complete(JsonParser.parseReader(reader).getAsJsonObject());
} finally {
connection.disconnect();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,7 @@ public void runAll(T event) {
}

public static <T extends Event> EventService<T> addService(ZUser user, Class<T> eventClass) {
if (EventService.hasService(user, eventClass)) {
throw new IllegalStateException(eventClass.getSimpleName() + " is already register for " + user.getUUID().toString());
}
if (EventService.hasService(user, eventClass)) throw new IllegalStateException(eventClass.getSimpleName() + " is already register for " + user.getUUID().toString());
EventService<T> service = new EventService<>(eventClass, new ArrayList<>());
user.getEventServices().add(service);
user.toPlayer().closeInventory();
Expand Down
Loading

0 comments on commit 983f240

Please sign in to comment.