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;
import java.io.Serial;
/**
* @author xCodiq - 20/04/2023
*/
public class CommandException extends Exception {
private static final long serialVersionUID = 1L;
@Serial private static final long serialVersionUID = 1L;
public CommandException(String 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 {
private final Class<? extends Entity> entityClass;
private final Map<String, Method> methods;
public CustomizationLoader(EntityType entityType, Iterable<String> methodsName) {
@ -28,16 +27,14 @@ public class CustomizationLoader {
protected Map<String, Method> loadMethods(Iterable<String> iterable) {
Map<String, Method> builder = new HashMap<>();
for (Method method : this.entityClass.getMethods()) {
if (!builder.containsKey(method.getName()) &&
Iterables.contains(iterable, method.getName())) {
if (builder.containsKey(method.getName()) || !Iterables.contains(iterable, method.getName())) continue;
for (Class<?> parameter : method.getParameterTypes()) {
TypeProperty typeProperty = TypeProperty.forType(parameter);
if (typeProperty == null && parameter.isEnum())
PrimitivePropertyType primitivePropertyType = PrimitivePropertyType.forType(parameter);
if (primitivePropertyType != null || !parameter.isEnum()) continue;
new EnumReflection(new ReflectionBuilder(ReflectionPackage.MINECRAFT).withClassName(parameter)).get();
}
builder.put(method.getName(), method);
}
}
return builder;
}

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

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

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

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

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

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

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

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

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

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