the refactoring never ends

This commit is contained in:
Pyrbu 2023-04-22 04:48:55 +01:00
parent 5395094063
commit e04ee865e1
13 changed files with 39 additions and 99 deletions

@ -1,11 +1,12 @@
package io.github.znetworkw.znpcservers.commands.exception; package io.github.znetworkw.znpcservers.commands.exception;
import java.io.Serial;
/** /**
* @author xCodiq - 20/04/2023 * @author xCodiq - 20/04/2023
*/ */
public class CommandException extends Exception { public class CommandException extends Exception {
@Serial private static final long serialVersionUID = 1L;
private static final long serialVersionUID = 1L;
public CommandException(String message) { public CommandException(String message) {
super(message); super(message);

@ -1,7 +0,0 @@
package io.github.znetworkw.znpcservers.commands.exception;
public class CommandNotFoundException extends CommandException {
public CommandNotFoundException(String message) {
super(message);
}
}

@ -13,7 +13,6 @@ import java.util.Map;
public class CustomizationLoader { public class CustomizationLoader {
private final Class<? extends Entity> entityClass; private final Class<? extends Entity> entityClass;
private final Map<String, Method> methods; private final Map<String, Method> methods;
public CustomizationLoader(EntityType entityType, Iterable<String> methodsName) { public CustomizationLoader(EntityType entityType, Iterable<String> methodsName) {
@ -28,15 +27,13 @@ public class CustomizationLoader {
protected Map<String, Method> loadMethods(Iterable<String> iterable) { protected Map<String, Method> loadMethods(Iterable<String> iterable) {
Map<String, Method> builder = new HashMap<>(); Map<String, Method> builder = new HashMap<>();
for (Method method : this.entityClass.getMethods()) { for (Method method : this.entityClass.getMethods()) {
if (!builder.containsKey(method.getName()) && if (builder.containsKey(method.getName()) || !Iterables.contains(iterable, method.getName())) continue;
Iterables.contains(iterable, method.getName())) { for (Class<?> parameter : method.getParameterTypes()) {
for (Class<?> parameter : method.getParameterTypes()) { PrimitivePropertyType primitivePropertyType = PrimitivePropertyType.forType(parameter);
TypeProperty typeProperty = TypeProperty.forType(parameter); if (primitivePropertyType != null || !parameter.isEnum()) continue;
if (typeProperty == null && parameter.isEnum()) new EnumReflection(new ReflectionBuilder(ReflectionPackage.MINECRAFT).withClassName(parameter)).get();
new EnumReflection(new ReflectionBuilder(ReflectionPackage.MINECRAFT).withClassName(parameter)).get();
}
builder.put(method.getName(), method);
} }
builder.put(method.getName(), method);
} }
return builder; return builder;
} }

@ -31,9 +31,7 @@ public interface NPCPath {
abstract class AbstractPath implements PathInitializer { abstract class AbstractPath implements PathInitializer {
private final NPC npc; private final NPC npc;
private final NPCPath.AbstractTypeWriter typeWriter; private final NPCPath.AbstractTypeWriter typeWriter;
private ZLocation location; private ZLocation location;
public AbstractPath(NPC npc, NPCPath.AbstractTypeWriter typeWriter) { public AbstractPath(NPC npc, NPCPath.AbstractTypeWriter typeWriter) {
@ -85,7 +83,6 @@ public interface NPCPath {
abstract class AbstractTypeWriter implements NPCPath { abstract class AbstractTypeWriter implements NPCPath {
private static final ConcurrentMap<String, AbstractTypeWriter> PATH_TYPES = new ConcurrentHashMap<>(); private static final ConcurrentMap<String, AbstractTypeWriter> PATH_TYPES = new ConcurrentHashMap<>();
private static final int PATH_DELAY = 1;
private final TypeWriter typeWriter; private final TypeWriter typeWriter;
private final File file; private final File file;
private final List<ZLocation> locationList; private final List<ZLocation> locationList;
@ -101,14 +98,12 @@ public interface NPCPath {
} }
public static AbstractTypeWriter forCreation(String pathName, ZUser user, TypeWriter typeWriter) { public static AbstractTypeWriter forCreation(String pathName, ZUser user, TypeWriter typeWriter) {
if (typeWriter == TypeWriter.MOVEMENT) if (typeWriter == TypeWriter.MOVEMENT) return new TypeMovement(pathName, user);
return new TypeMovement(pathName, user);
throw new IllegalStateException("can't find type writer for: " + typeWriter.name()); throw new IllegalStateException("can't find type writer for: " + typeWriter.name());
} }
public static AbstractTypeWriter forFile(File file, TypeWriter typeWriter) { public static AbstractTypeWriter forFile(File file, TypeWriter typeWriter) {
if (typeWriter == TypeWriter.MOVEMENT) if (typeWriter == TypeWriter.MOVEMENT) return new TypeMovement(file);
return new TypeMovement(file);
throw new IllegalStateException("can't find type writer for: " + typeWriter.name()); throw new IllegalStateException("can't find type writer for: " + typeWriter.name());
} }
@ -125,22 +120,9 @@ public interface NPCPath {
} }
public void load() { public void load() {
try { try (DataInputStream reader = NPCPath.ZNPCPathDelegator.forFile(this.file).getInputStream()) {
DataInputStream reader = NPCPath.ZNPCPathDelegator.forFile(this.file).getInputStream(); initialize(reader);
try { register(this);
initialize(reader);
register(this);
if (reader != null)
reader.close();
} catch (Throwable throwable) {
if (reader != null)
try {
reader.close();
} catch (Throwable throwable1) {
throwable.addSuppressed(throwable1);
}
throw throwable;
}
} catch (IOException e) { } catch (IOException e) {
ZNPCsPlus.LOGGER.warning("[AbstractTypeWriter] " + String.format("The path %s could not be loaded", this.file.getName())); ZNPCsPlus.LOGGER.warning("[AbstractTypeWriter] " + String.format("The path %s could not be loaded", this.file.getName()));
e.printStackTrace(); e.printStackTrace();
@ -148,21 +130,9 @@ public interface NPCPath {
} }
public void write() { public void write() {
try { try (DataOutputStream writer = NPCPath.ZNPCPathDelegator.forFile(getFile()).getOutputStream()) {
DataOutputStream writer = NPCPath.ZNPCPathDelegator.forFile(getFile()).getOutputStream(); write(writer);
try { if (writer != null) writer.close();
write(writer);
if (writer != null)
writer.close();
} catch (Throwable throwable) {
if (writer != null)
try {
writer.close();
} catch (Throwable throwable1) {
throwable.addSuppressed(throwable1);
}
throw throwable;
}
} catch (IOException e) { } catch (IOException e) {
ZNPCsPlus.LOGGER.warning("[AbstractTypeWriter] " + String.format("Path %s could not be created", getName())); ZNPCsPlus.LOGGER.warning("[AbstractTypeWriter] " + String.format("Path %s could not be created", getName()));
e.printStackTrace(); e.printStackTrace();
@ -187,9 +157,7 @@ public interface NPCPath {
private static class TypeMovement extends AbstractTypeWriter { private static class TypeMovement extends AbstractTypeWriter {
private static final int MAX_LOCATIONS = ((Integer) Configuration.CONFIGURATION.getValue(ConfigurationValue.MAX_PATH_LOCATIONS)).intValue(); private static final int MAX_LOCATIONS = ((Integer) Configuration.CONFIGURATION.getValue(ConfigurationValue.MAX_PATH_LOCATIONS)).intValue();
private ZUser npcUser; private ZUser npcUser;
private BukkitTask bukkitTask; private BukkitTask bukkitTask;
public TypeMovement(File file) { public TypeMovement(File file) {
@ -215,8 +183,7 @@ public interface NPCPath {
} }
public void write(DataOutputStream dataOutputStream) throws IOException { public void write(DataOutputStream dataOutputStream) throws IOException {
if (getLocationList().isEmpty()) if (getLocationList().isEmpty()) return;
return;
Iterator<ZLocation> locationIterator = getLocationList().iterator(); Iterator<ZLocation> locationIterator = getLocationList().iterator();
while (locationIterator.hasNext()) { while (locationIterator.hasNext()) {
ZLocation location = locationIterator.next(); ZLocation location = locationIterator.next();
@ -226,8 +193,7 @@ public interface NPCPath {
dataOutputStream.writeDouble(location.getZ()); dataOutputStream.writeDouble(location.getZ());
dataOutputStream.writeFloat(location.getYaw()); dataOutputStream.writeFloat(location.getYaw());
dataOutputStream.writeFloat(location.getPitch()); dataOutputStream.writeFloat(location.getPitch());
if (!locationIterator.hasNext()) if (!locationIterator.hasNext()) register(this);
register(this);
} }
} }
@ -262,7 +228,6 @@ public interface NPCPath {
protected static class MovementPath extends NPCPath.PathInitializer.AbstractPath { protected static class MovementPath extends NPCPath.PathInitializer.AbstractPath {
private int currentEntryPath = 0; private int currentEntryPath = 0;
private boolean pathReverse = false; private boolean pathReverse = false;
public MovementPath(NPC npc, NPCPath.AbstractTypeWriter.TypeMovement path) { public MovementPath(NPC npc, NPCPath.AbstractTypeWriter.TypeMovement path) {

@ -10,8 +10,7 @@ public class NPCSkin {
private final String signature; private final String signature;
protected NPCSkin(String... values) { protected NPCSkin(String... values) {
if (values.length < 1) if (values.length < 1) throw new IllegalArgumentException("Invalid arguments for NPC skin constructor");
throw new IllegalArgumentException("Length cannot be zero or negative.");
this.texture = values[0]; this.texture = values[0];
this.signature = values[1]; this.signature = values[1];
} }

@ -116,8 +116,8 @@ public enum NPCType {
Class<?>[] methodParameterTypes = method.getParameterTypes(); Class<?>[] methodParameterTypes = method.getParameterTypes();
Object[] newArray = new Object[methodParameterTypes.length]; Object[] newArray = new Object[methodParameterTypes.length];
for (int i = 0; i < methodParameterTypes.length; ++i) { for (int i = 0; i < methodParameterTypes.length; ++i) {
TypeProperty typeProperty = TypeProperty.forType(methodParameterTypes[i]); PrimitivePropertyType primitivePropertyType = PrimitivePropertyType.forType(methodParameterTypes[i]);
newArray[i] = typeProperty != null ? typeProperty.getFunction().apply(strings[i]) : EnumPropertyCache.find(strings[i], methodParameterTypes[i]); newArray[i] = primitivePropertyType != null ? primitivePropertyType.getFunction().apply(strings[i]) : EnumPropertyCache.find(strings[i], methodParameterTypes[i]);
} }
return newArray; return newArray;
} }

@ -2,7 +2,7 @@ package io.github.znetworkw.znpcservers.npc;
import java.util.function.Function; import java.util.function.Function;
public enum TypeProperty { public enum PrimitivePropertyType {
STRING(String::toString), STRING(String::toString),
BOOLEAN(Boolean::parseBoolean), BOOLEAN(Boolean::parseBoolean),
INT(Integer::parseInt), INT(Integer::parseInt),
@ -13,11 +13,11 @@ public enum TypeProperty {
private final Function<String, ?> function; private final Function<String, ?> function;
TypeProperty(Function<String, ?> function) { PrimitivePropertyType(Function<String, ?> function) {
this.function = function; this.function = function;
} }
public static TypeProperty forType(Class<?> primitiveType) { public static PrimitivePropertyType forType(Class<?> primitiveType) {
if (primitiveType == String.class) return STRING; if (primitiveType == String.class) return STRING;
if (primitiveType == boolean.class) return BOOLEAN; if (primitiveType == boolean.class) return BOOLEAN;
if (primitiveType == int.class) return INT; if (primitiveType == int.class) return INT;

@ -20,8 +20,7 @@ public class ConversationKey {
} }
public ConversationKey(Iterable<String> line) { public ConversationKey(Iterable<String> line) {
this this.lines = StreamSupport.stream(line.spliterator(), false).map(String::toString).collect(Collectors.toList());
.lines = StreamSupport.stream(line.spliterator(), false).map(String::toString).collect(Collectors.toList());
this.actions = new ArrayList<>(); this.actions = new ArrayList<>();
} }
@ -50,8 +49,7 @@ public class ConversationKey {
} }
public String getTextFormatted() { public String getTextFormatted() {
if (this.lines.isEmpty()) if (this.lines.isEmpty()) return "";
return "";
String text = this.lines.iterator().next(); String text = this.lines.iterator().next();
int fixedLength = Math.min(text.length(), 28); int fixedLength = Math.min(text.length(), 28);
return text.substring(0, fixedLength); return text.substring(0, fixedLength);

@ -22,6 +22,7 @@ public class ConversationModel {
} }
} }
@SuppressWarnings("unused")
private ConversationModel() { private ConversationModel() {
} }
@ -38,14 +39,11 @@ public class ConversationModel {
} }
public void startConversation(NPC npc, Player player) { public void startConversation(NPC npc, Player player) {
if (!Conversation.exists(this.conversationName)) if (!Conversation.exists(this.conversationName)) throw new IllegalStateException("can't find conversation " + this.conversationName);
throw new IllegalStateException("can't find conversation " + this.conversationName); if (ConversationProcessor.isPlayerConversing(player.getUniqueId())) return;
if (ConversationProcessor.isPlayerConversing(player.getUniqueId()))
return;
if (this.lastStarted.containsKey(player.getUniqueId())) { if (this.lastStarted.containsKey(player.getUniqueId())) {
long lastConversationNanos = System.nanoTime() - this.lastStarted.get(player.getUniqueId()); long lastConversationNanos = System.nanoTime() - this.lastStarted.get(player.getUniqueId());
if (lastConversationNanos < 1000000000L * getConversation().getDelay()) if (lastConversationNanos < 1000000000L * getConversation().getDelay()) return;
return;
} }
this.lastStarted.remove(player.getUniqueId()); this.lastStarted.remove(player.getUniqueId());
if (this.conversationType.canStart(npc, getConversation(), player)) { if (this.conversationType.canStart(npc, getConversation(), player)) {
@ -61,8 +59,7 @@ public class ConversationModel {
public enum ConversationType { public enum ConversationType {
RADIUS { RADIUS {
public boolean canStart(NPC npc, Conversation conversation, Player player) { public boolean canStart(NPC npc, Conversation conversation, Player player) {
return (player.getWorld() == npc.getLocation().getWorld() && player return (player.getWorld() == npc.getLocation().getWorld() && player.getLocation().distance(npc.getLocation()) <= conversation.getRadius());
.getLocation().distance(npc.getLocation()) <= conversation.getRadius());
} }
}, },
CLICK { CLICK {

@ -4,10 +4,8 @@ public enum ClickType {
RIGHT, LEFT, DEFAULT; RIGHT, LEFT, DEFAULT;
public static ClickType forName(String clickName) { public static ClickType forName(String clickName) {
if (clickName.startsWith("INTERACT")) if (clickName.startsWith("INTERACT")) return RIGHT;
return RIGHT; if (clickName.startsWith("ATTACK")) return LEFT;
if (clickName.startsWith("ATTACK"))
return LEFT;
return DEFAULT; return DEFAULT;
} }
} }

@ -18,11 +18,8 @@ import java.util.List;
public class Hologram { public class Hologram {
private static final boolean NEW_METHOD = (Utils.BUKKIT_VERSION > 12); private static final boolean NEW_METHOD = (Utils.BUKKIT_VERSION > 12);
private static final double LINE_SPACING = Configuration.CONFIGURATION.getValue(ConfigurationValue.LINE_SPACING); private static final double LINE_SPACING = Configuration.CONFIGURATION.getValue(ConfigurationValue.LINE_SPACING);
private final List<HologramLine> hologramLines = new ArrayList<>(); private final List<HologramLine> hologramLines = new ArrayList<>();
private final NPC npc; private final NPC npc;
public Hologram(NPC npc) { public Hologram(NPC npc) {

@ -12,9 +12,7 @@ public interface LineReplacer {
if (!lineReplacer.isSupported()) continue; if (!lineReplacer.isSupported()) continue;
string = lineReplacer.make(string); string = lineReplacer.make(string);
} }
return Utils.toColor((Utils.PLACEHOLDER_SUPPORT && user != null) ? return Utils.toColor((Utils.PLACEHOLDER_SUPPORT && user != null) ? Utils.getWithPlaceholders(string, user.toPlayer()) : string);
Utils.getWithPlaceholders(string, user.toPlayer()) :
string);
} }
String make(String paramString); String make(String paramString);

@ -21,14 +21,11 @@ public class RGBLine implements LineReplacer {
break; break;
} }
char hexCode = rgbString.charAt(i2); char hexCode = rgbString.charAt(i2);
hexCodeStringBuilder.append((ConfigurationConstants.RGB_ANIMATION && hexCode != '#') ? hexCodeStringBuilder.append((ConfigurationConstants.RGB_ANIMATION && hexCode != '#') ? Integer.toHexString(ThreadLocalRandom.current().nextInt(16)) : Character.valueOf(hexCode));
Integer.toHexString(ThreadLocalRandom.current().nextInt(16)) : Character.valueOf(hexCode));
} }
if (success) if (success) try {
try { rgbString = rgbString.substring(0, i) + ChatColor.of(hexCodeStringBuilder.toString()) + rgbString.substring(endIndex);
rgbString = rgbString.substring(0, i) + ChatColor.of(hexCodeStringBuilder.toString()) + rgbString.substring(endIndex); } catch (Exception ignored) {}
} catch (Exception ignored) {
}
} }
} }
return rgbString; return rgbString;