Skip to content
This repository was archived by the owner on Jun 1, 2024. It is now read-only.

Commit

Permalink
Optimizations, Small Bug Fixes
Browse files Browse the repository at this point in the history
- Remove Unused Code
- Small Bug Fixes
- Improve Maintainability
- Fix NBTSection1_20_R4 on 1.20.5
  • Loading branch information
gmitch215 committed Apr 26, 2024
1 parent 0328112 commit ae1baad
Show file tree
Hide file tree
Showing 13 changed files with 87 additions and 73 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ default void clearPathfinders(Mob mob, boolean target) {
getGoals(mob, target).forEach(w -> removePathfinder(w.getPathfinder(), target));
}

default void addPathfinders(Collection<? extends WrappedPathfinder> c, boolean target) {
default void addPathfinders(Collection<WrappedPathfinder> c, boolean target) {
for (WrappedPathfinder p : c) addPathfinder(p.getPathfinder(), p.getPriority(), target);
}

Expand Down Expand Up @@ -178,7 +178,7 @@ static Class<?>[] getArgTypes(Object... args) {
return types;
}

static String bukkitToCraftBukkkit() {
static String bukkitToCraftBukkit() {
String bukkit = Bukkit.getServer().getBukkitVersion().split("-")[0];
switch (bukkit) {
case "1.20.1":
Expand All @@ -200,7 +200,7 @@ static String getServerVersion() {
return Bukkit.getServer().getClass().getPackage().getName().split("\\.")[3].substring(1);
} catch (IndexOutOfBoundsException e) {
// Using CraftBukkit Relocation
return bukkitToCraftBukkkit();
return bukkitToCraftBukkit();
}
}

Expand All @@ -219,7 +219,8 @@ static ChipUtil getWrapper() {

static void printStackTrace(Throwable e) {
Bukkit.getLogger().severe(e.getClass().getName() + ": " + e.getMessage());
for (StackTraceElement s : e.getStackTrace()) Bukkit.getLogger().severe(" " + s.toString());
for (StackTraceElement s : e.getStackTrace())
Bukkit.getLogger().severe(s.toString());
if (e.getCause() != null) {
Bukkit.getLogger().severe("Caused by:");
printStackTrace(e.getCause());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ public abstract class BehaviorResult {
return Status.STOPPED;
}
@Override
public void stop() {}
public void stop() {
// Do nothing
}
};

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public PathfinderBeg(@NotNull Wolf w) {
*/
public PathfinderBeg(@NotNull Wolf w, float lookRange) {
super(w);
this.lookRange = lookRange;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,10 @@ public void setItems(@NotNull Iterable<? extends ItemStack> items) throws Illega
* @param items Array of Items to use
* @throws IllegalArgumentException if Items are null or empty
*/
public void setItems(@NotNull ItemStack... items) throws IllegalArgumentException
{ if (items == null) throw new IllegalArgumentException("Items cannot be null"); setItems(Arrays.asList(items)); }
public void setItems(@NotNull ItemStack... items) throws IllegalArgumentException {
if (items == null) throw new IllegalArgumentException("Items cannot be null");
setItems(Arrays.asList(items));
}

@Override
public double getSpeedModifier() {
Expand Down
9 changes: 3 additions & 6 deletions base/src/main/java/me/gamercoder215/mobchip/bosses/Boss.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,7 @@

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.*;

/**
* Represents a Boss Entity.
Expand All @@ -51,7 +48,7 @@ public abstract class Boss<T extends Mob> {
private float pitch;

private double health = DEFAULT_HEALTH;
private final Map<EquipmentSlot, ItemStack> equipment = new HashMap<>();
private final Map<EquipmentSlot, ItemStack> equipment = new EnumMap<>(EquipmentSlot.class);

private final Map<AttributeInstance, Double> attributes = new HashMap<>();

Expand Down Expand Up @@ -237,7 +234,7 @@ public void run() {
Bukkit.getLogger().severe(e.getCause().getMessage());
for (StackTraceElement s : e.getCause().getStackTrace()) Bukkit.getLogger().severe(s.toString());
} catch (Exception e) {
Bukkit.getLogger().severe(e.getMessage());
Bukkit.getLogger().severe(e.getMessage());
for (StackTraceElement s : e.getStackTrace()) Bukkit.getLogger().severe(s.toString());
}
}
Expand Down
14 changes: 5 additions & 9 deletions base/src/main/java/me/gamercoder215/mobchip/util/Position.java
Original file line number Diff line number Diff line change
Expand Up @@ -87,11 +87,11 @@ public double distance(@NotNull Entity en) {
* @return distance squared
*/
public double distanceSquared(@NotNull Position node) {
double x = this.x - node.x;
double y = this.y - node.y;
double z = this.z - node.z;
int x = this.x - node.x;
int y = this.y - node.y;
int z = this.z - node.z;

return x * x + y * y + z * z;
return (x * x) + (y * y) + (z * z);
}

/**
Expand Down Expand Up @@ -309,10 +309,6 @@ public Position setZ(int z) {

@Override
public Position clone() {
try {
return (Position) super.clone();
} catch (CloneNotSupportedException e) {
throw new Error(e);
}
return new Position(x, y, z);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,9 @@ public EntityBehavior getBehaviors() {
* @param <T> Memory Type
*/
@Override
public <T> void setMemory(@NotNull Memory<T> memory, @NotNull T value) throws IllegalArgumentException {
public <T> void setMemory(@NotNull Memory<T> memory, @Nullable T value) throws IllegalArgumentException {
if (value == null) removeMemory(memory);

Object old = getMemory(memory);
w.setMemory(m, memory, value);

Expand All @@ -278,7 +280,9 @@ public <T> void setMemory(@NotNull Memory<T> memory, @NotNull T value) throws Il
* @param <T> Memory Type
*/
@Override
public <T> void setMemory(@NotNull Memory<T> memory, @NotNull T value, long expire) throws IllegalArgumentException {
public <T> void setMemory(@NotNull Memory<T> memory, @Nullable T value, long expire) throws IllegalArgumentException {
if (value == null) removeMemory(memory);

Object old = getMemory(memory);
w.setMemory(m, memory, value, expire);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
package me.gamercoder215.mobchip.bukkit.events;

import me.gamercoder215.mobchip.EntityBrain;
import org.bukkit.event.Event;
import org.bukkit.event.HandlerList;
import org.jetbrains.annotations.NotNull;

import me.gamercoder215.mobchip.EntityBrain;

/**
* Represents an Event related to the Brain
*/
Expand All @@ -18,7 +17,7 @@ public abstract class BrainEvent extends Event {
* Construct a BrainEvent
* @param brain EntityBrain involved
*/
public BrainEvent(@NotNull EntityBrain brain) {
protected BrainEvent(@NotNull EntityBrain brain) {
this.brain = brain;
}

Expand All @@ -27,7 +26,7 @@ public BrainEvent(@NotNull EntityBrain brain) {
* @param brain EntityBrain involved
* @param async true if async, else false
*/
public BrainEvent(@NotNull EntityBrain brain, boolean async) {
protected BrainEvent(@NotNull EntityBrain brain, boolean async) {
super(async);

this.brain = brain;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ protected void start(WorldServer w, EntityCreature c, long var2) {
c.getBehaviorController().removeMemory(MemoryModuleType.WALK_TARGET);
}

protected void stop(WorldServer w, EntityCreature c, long var2) {}
protected void stop(WorldServer w, EntityCreature c, long var2) {
// Do nothing
}

protected void tick(WorldServer w, EntityCreature c, long var2) {
if (c.getNavigation().n()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ protected void start(WorldServer w, EntityCreature c, long var2) {
c.getBehaviorController().removeMemory(MemoryModuleType.WALK_TARGET);
}

protected void stop(WorldServer w, EntityCreature c, long var2) {}
protected void stop(WorldServer w, EntityCreature c, long var2) {
// Do nothing
}

protected void tick(WorldServer w, EntityCreature c, long var2) {
if (c.getNavigation().m()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ protected void start(WorldServer w, EntityCreature c, long var2) {
c.getBehaviorController().removeMemory(MemoryModuleType.WALK_TARGET);
}

protected void stop(WorldServer w, EntityCreature c, long var2) {}
protected void stop(WorldServer w, EntityCreature c, long var2) {
// Do nothing
}

protected void tick(WorldServer w, EntityCreature c, long var2) {
if (c.getNavigation().m()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ protected void start(WorldServer w, EntityCreature c, long var2) {
c.getBehaviorController().removeMemory(MemoryModuleType.WALK_TARGET);
}

protected void stop(WorldServer w, EntityCreature c, long var2) {}
protected void stop(WorldServer w, EntityCreature c, long var2) {
// Do nothing
}

protected void tick(WorldServer w, EntityCreature c, long var2) {
if (c.getNavigation().m()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
import net.minecraft.nbt.*;
import org.bukkit.*;
import org.bukkit.configuration.serialization.ConfigurationSerializable;
import org.bukkit.craftbukkit.v1_20_R3.inventory.CraftItemStack;
import org.bukkit.craftbukkit.v1_20_R4.CraftRegistry;
import org.bukkit.craftbukkit.v1_20_R4.inventory.CraftItemStack;
import org.bukkit.entity.Mob;
import org.bukkit.inventory.ItemStack;
import org.bukkit.util.EulerAngle;
Expand Down Expand Up @@ -50,48 +51,49 @@ static Tag serialize(Object v) {
return tag;
}

if (v instanceof Collection<?>) {
List<?> collection = new ArrayList<>((Collection<?>) v);
CompoundTag coll = new CompoundTag();
switch (v) {
case Collection<?> objects -> {
List<?> collection = new ArrayList<>(objects);
CompoundTag coll = new CompoundTag();

try {
coll.putString(ChipUtil.CLASS_TAG, Collection.class.getName());
ListTag tag = new ListTag();
for (int i = 0; i < collection.size(); i++) tag.add(i, serialize(collection.get(i)));
coll.put("values", tag);

Field idF = ListTag.class.getDeclaredField("w");
idF.setAccessible(true);
try {
coll.putString(ChipUtil.CLASS_TAG, Collection.class.getName());
ListTag tag = new ListTag();
for (int i = 0; i < collection.size(); i++) tag.add(i, serialize(collection.get(i)));
coll.put("values", tag);

Field idF = ListTag.class.getDeclaredField("w");
idF.setAccessible(true);

coll.putByte("id", idF.getByte(tag));
} catch (ReflectiveOperationException e) {
Bukkit.getLogger().severe("Failed to serialize collection: " + e.getMessage());
for (StackTraceElement ste : e.getStackTrace()) Bukkit.getLogger().severe(ste.toString());
}

coll.putByte("id", idF.getByte(tag));
} catch (ReflectiveOperationException e) {
Bukkit.getLogger().severe("Failed to serialize collection: " + e.getMessage());
for (StackTraceElement ste : e.getStackTrace()) Bukkit.getLogger().severe(ste.toString());
return coll;
}

return coll;
}

if (v instanceof Map<?, ?> map) {
CompoundTag tag = new CompoundTag();
for (Map.Entry<?, ?> entry : map.entrySet()) {
tag.put(entry.getKey().toString(), serialize(entry.getValue()));
case Map<?, ?> map -> {
CompoundTag tag = new CompoundTag();
for (Map.Entry<?, ?> entry : map.entrySet()) {
tag.put(entry.getKey().toString(), serialize(entry.getValue()));
}
return tag;
}
case Enum<?> enumeration -> {
CompoundTag tag = new CompoundTag();
tag.putString(ChipUtil.CLASS_TAG, enumeration.getClass().getName());
tag.putString("value", ((Enum<?>) v).name());
return tag;
}
case ConfigurationSerializable serializable -> {
CompoundTag tag = new CompoundTag();
tag.putString(ChipUtil.CLASS_TAG, serializable.getClass().getName());
tag.put("value", serialize(serializable.serialize()));
return tag;
}
default -> {
}
return tag;
}

if (v instanceof Enum<?> enumeration) {
CompoundTag tag = new CompoundTag();
tag.putString(ChipUtil.CLASS_TAG, enumeration.getClass().getName());
tag.putString("value", ((Enum<?>) v).name());
return tag;
}

if (v instanceof ConfigurationSerializable serializable) {
CompoundTag tag = new CompoundTag();
tag.putString(ChipUtil.CLASS_TAG, serializable.getClass().getName());
tag.put("value", serialize(serializable.serialize()));
return tag;
}

return switch (v.getClass().getSimpleName().toLowerCase()) {
Expand Down Expand Up @@ -121,7 +123,7 @@ static Tag serialize(Object v) {
ItemStack item = (ItemStack) v;
CompoundTag stack = new CompoundTag();
stack.putString(ChipUtil.CLASS_TAG, item.getClass().getName());
stack.put("item", CraftItemStack.asNMSCopy(item).getOrCreateTag());
stack.put("item", CraftItemStack.asNMSCopy(item).saveOptional(CraftRegistry.getMinecraftRegistry()));

yield stack;
}
Expand Down Expand Up @@ -238,7 +240,9 @@ static Object deserialize(Tag v) {
}
case "itemstack" -> {
CompoundTag item = cmp.getCompound("item");
yield CraftItemStack.asBukkitCopy(net.minecraft.world.item.ItemStack.of(item));
Optional<ItemStack> stack = net.minecraft.world.item.ItemStack.parse(CraftRegistry.getMinecraftRegistry(), item)
.map(CraftItemStack::asBukkitCopy);
yield stack.orElse(null);
}
case "location" -> {
String world = cmp.getString("world");
Expand Down

0 comments on commit ae1baad

Please sign in to comment.