Skip to content

Commit

Permalink
Initial 1.19.4 update. Of note: sniffers are known to not be working
Browse files Browse the repository at this point in the history
  • Loading branch information
fullwall committed Mar 14, 2023
1 parent e6f8a47 commit 299f781
Show file tree
Hide file tree
Showing 162 changed files with 2,549 additions and 1,686 deletions.
4 changes: 2 additions & 2 deletions main/pom.xml
Expand Up @@ -4,12 +4,12 @@
<parent>
<groupId>net.citizensnpcs</groupId>
<artifactId>citizens-parent</artifactId>
<version>2.0.30-SNAPSHOT</version>
<version>2.0.31-SNAPSHOT</version>
</parent>
<artifactId>citizens-main</artifactId>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<craftbukkit.version>1.19.3-R0.1-SNAPSHOT</craftbukkit.version>
<craftbukkit.version>1.19.4-R0.1-SNAPSHOT</craftbukkit.version>
<placeholderapi.version>2.11.2</placeholderapi.version>
<citizensapi.version>${project.version}</citizensapi.version>
<worldguard.version>7.0.4</worldguard.version>
Expand Down
4 changes: 2 additions & 2 deletions main/src/main/java/net/citizensnpcs/npc/CitizensNPC.java
Expand Up @@ -191,8 +191,8 @@ public void load(final DataKey root) {

@Override
public boolean requiresNameHologram() {
return super.requiresNameHologram()
|| (getEntityType() != EntityType.ARMOR_STAND && Setting.ALWAYS_USE_NAME_HOLOGRAM.asBoolean());
return super.requiresNameHologram() || (getEntityType() != EntityType.ARMOR_STAND
&& !getEntityType().name().equals("TEXT_DISPLAY") && Setting.ALWAYS_USE_NAME_HOLOGRAM.asBoolean());
}

private void resetCachedCoord() {
Expand Down
32 changes: 23 additions & 9 deletions main/src/main/java/net/citizensnpcs/trait/HologramTrait.java
@@ -1,6 +1,7 @@
package net.citizensnpcs.trait;

import java.util.Collection;
import java.util.IllegalFormatException;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
Expand All @@ -9,7 +10,7 @@
import org.bukkit.ChatColor;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.entity.ArmorStand;
import org.bukkit.entity.Entity;
import org.bukkit.entity.EntityType;
import org.bukkit.event.player.PlayerTeleportEvent.TeleportCause;
import org.bukkit.inventory.ItemStack;
Expand Down Expand Up @@ -87,8 +88,13 @@ public void clear() {
}

private NPC createHologram(String line, double heightOffset) {
NPC hologramNPC = registry.createNPC(EntityType.ARMOR_STAND, line);
hologramNPC.getOrAddTrait(ArmorStandTrait.class).setAsHelperEntityWithName(npc);
NPC hologramNPC = null;
if (SUPPORTS_TEXT_DISPLAY) {
hologramNPC = registry.createNPC(EntityType.TEXT_DISPLAY, line);
} else {
hologramNPC = registry.createNPC(EntityType.ARMOR_STAND, line);
hologramNPC.getOrAddTrait(ArmorStandTrait.class).setAsHelperEntityWithName(npc);
}
if (Setting.PACKET_HOLOGRAMS.asBoolean()) {
hologramNPC.addTrait(PacketNPC.class);
}
Expand All @@ -107,7 +113,7 @@ private NPC createHologram(String line, double heightOffset) {
.setColor(Util.matchEnum(ChatColor.values(), itemMatcher.group(2).substring(1)));
}
itemNPC.spawn(currentLoc);
((ArmorStand) hologramNPC.getEntity()).addPassenger(itemNPC.getEntity());
hologramNPC.getEntity().addPassenger(itemNPC.getEntity());
itemNPC.addRunnable(new Runnable() {
@Override
public void run() {
Expand Down Expand Up @@ -147,9 +153,9 @@ private double getHeight(int lineNumber) {
/**
* Note: this is implementation-specific and may be removed at a later date.
*/
public Collection<ArmorStand> getHologramEntities() {
public Collection<Entity> getHologramEntities() {
return lines.stream().filter(l -> l.hologram != null && l.hologram.getEntity() != null)
.map(l -> (ArmorStand) l.hologram.getEntity()).collect(Collectors.toList());
.map(l -> l.hologram.getEntity()).collect(Collectors.toList());
}

/**
Expand All @@ -173,8 +179,8 @@ private double getMaxHeight() {
/**
* Note: this is implementation-specific and may be removed at a later date.
*/
public ArmorStand getNameEntity() {
return nameNPC != null && nameNPC.isSpawned() ? ((ArmorStand) nameNPC.getEntity()) : null;
public Entity getNameEntity() {
return nameNPC != null && nameNPC.isSpawned() ? nameNPC.getEntity() : null;
}

@Override
Expand Down Expand Up @@ -318,7 +324,7 @@ public void run() {

if (!updateName)
continue;
hologramNPC.setName(Placeholders.replace(text, null, npc));
line.setText(text);
hologramNPC.data().set(NPC.Metadata.NAMEPLATE_VISIBLE, npc.getRawName().length() > 0);
}
}
Expand Down Expand Up @@ -428,4 +434,12 @@ public void spawnNPC(double height) {
}

private static final Pattern ITEM_MATCHER = Pattern.compile("<item:(.*?)([:].*?)?>");
private static boolean SUPPORTS_TEXT_DISPLAY = true;
static {
try {
EntityType.valueOf("TEXT_DISPLAY");
} catch (IllegalFormatException e) {
SUPPORTS_TEXT_DISPLAY = false;
}
}
}
@@ -0,0 +1,78 @@
package net.citizensnpcs.trait.versioned;

import org.bukkit.command.CommandSender;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.Sniffer;

import net.citizensnpcs.api.command.Command;
import net.citizensnpcs.api.command.CommandContext;
import net.citizensnpcs.api.command.Flag;
import net.citizensnpcs.api.command.Requirements;
import net.citizensnpcs.api.command.exception.CommandException;
import net.citizensnpcs.api.command.exception.CommandUsageException;
import net.citizensnpcs.api.npc.NPC;
import net.citizensnpcs.api.persistence.Persist;
import net.citizensnpcs.api.trait.Trait;
import net.citizensnpcs.api.trait.TraitName;
import net.citizensnpcs.api.util.Messaging;
import net.citizensnpcs.util.Messages;
import net.citizensnpcs.util.NMS;

@TraitName("sniffertrait")
public class SnifferTrait extends Trait {
@Persist
private SnifferState state = null;

public SnifferTrait() {
super("sniffertrait");
}

public SnifferState getState() {
return state;
}

@Override
public void run() {
if (!npc.isSpawned() || !(npc.getEntity() instanceof Sniffer))
return;
NMS.setSnifferState(npc.getEntity(), state);
}

public void setState(SnifferState state) {
this.state = state;
}

public enum SnifferState {
DIGGING,
FEELING_HAPPY,
IDLING,
RISING,
SCENTING,
SEARCHING,
SNIFFING
}

@Command(
aliases = { "npc" },
usage = "sniffer (--state [state])",
desc = "Sets sniffer modifiers",
modifiers = { "sniffer" },
min = 1,
max = 1,
permission = "citizens.npc.sniffer")
@Requirements(selected = true, ownership = true, types = EntityType.SNIFFER)
public static void sniffer(CommandContext args, CommandSender sender, NPC npc, @Flag("state") SnifferState state)
throws CommandException {
SnifferTrait trait = npc.getOrAddTrait(SnifferTrait.class);
String output = "";
if (state != null) {
trait.setState(state);
output += ' ' + Messaging.tr(Messages.SNIFFER_STATE_SET, npc.getName(), state);
}
if (!output.isEmpty()) {
Messaging.send(sender, output.trim());
} else {
throw new CommandUsageException();
}
}
}
1 change: 1 addition & 0 deletions main/src/main/java/net/citizensnpcs/util/Messages.java
Expand Up @@ -363,6 +363,7 @@ public class Messages {
public static final String SKIPPING_BROKEN_TRAIT = "citizens.notifications.skipping-broken-trait";
public static final String SKIPPING_INVALID_ANCHOR = "citizens.notifications.skipping-invalid-anchor";
public static final String SKIPPING_INVALID_POSE = "citizens.notifications.skipping-invalid-pose";
public static final String SNIFFER_STATE_SET = "citizens.commands.npc.sniffer.state-set";
public static final String SNOWMAN_DERP_SET = "citizens.commands.npc.snowman.derp-set";
public static final String SNOWMAN_DERP_STOPPED = "citizens.commands.npc.snowman.derp-stopped";
public static final String SOUND_INFO = "citizens.commands.npc.sound.info";
Expand Down
75 changes: 47 additions & 28 deletions main/src/main/java/net/citizensnpcs/util/NMS.java
Expand Up @@ -9,6 +9,7 @@
import java.util.List;
import java.util.function.Consumer;
import java.util.function.Supplier;
import java.util.stream.Collectors;

import org.bukkit.Bukkit;
import org.bukkit.Location;
Expand All @@ -31,6 +32,7 @@
import org.bukkit.scoreboard.Team;
import org.bukkit.util.Vector;

import com.google.common.collect.Lists;
import com.mojang.authlib.GameProfile;
import com.mojang.authlib.GameProfileRepository;

Expand All @@ -52,6 +54,7 @@
import net.citizensnpcs.npc.ai.MCTargetStrategy.TargetNavigator;
import net.citizensnpcs.npc.skin.SkinnableEntity;
import net.citizensnpcs.trait.versioned.CamelTrait.CamelPose;
import net.citizensnpcs.trait.versioned.SnifferTrait.SnifferState;

public class NMS {
private NMS() {
Expand Down Expand Up @@ -148,6 +151,33 @@ public static Field getField(Class<?> clazz, String field, boolean log) {
}
}

private static List<Field> getFieldsMatchingType(Class<?> clazz, Class<?> type, boolean allowStatic) {
List<Field> found = Lists.newArrayList();
for (Field field : clazz.getDeclaredFields()) {
if (allowStatic ^ Modifier.isStatic(field.getModifiers()))
continue;
if (field.getType() == type) {
found.add(field);
field.setAccessible(true);
}
}
return found;
}

public static List<MethodHandle> getFieldsOfType(Class<?> clazz, Class<?> type) {
List<Field> found = getFieldsMatchingType(clazz, type, false);
if (found.isEmpty())
return null;
return found.stream().map(f -> {
try {
return LOOKUP.unreflectGetter(f);
} catch (Throwable e) {
e.printStackTrace();
}
return null;
}).filter(f -> f != null).collect(Collectors.toList());
}

public static MethodHandle getFinalSetter(Class<?> clazz, String field) {
return getFinalSetter(clazz, field, true);
}
Expand Down Expand Up @@ -212,28 +242,12 @@ public static MethodHandle getFinalSetter(Class<?> clazz, String fieldName, bool
return null;
}

private static Field getFirstFieldMatchingType(Class<?> clazz, Class<?> type, boolean allowStatic) {
Field found = null;
for (Field field : clazz.getDeclaredFields()) {
if (allowStatic ^ Modifier.isStatic(field.getModifiers()))
continue;
if (field.getType() == type) {
found = field;
break;
}
}
if (found != null) {
found.setAccessible(true);
}
return found;
}

public static MethodHandle getFirstFinalSetter(Class<?> clazz, Class<?> type) {
try {
Field found = getFirstFieldMatchingType(clazz, type, false);
if (found == null)
List<Field> found = getFieldsMatchingType(clazz, type, false);
if (found.isEmpty())
return null;
return getFinalSetter(clazz, found.getName());
return getFinalSetter(clazz, found.get(0).getName());
} catch (Exception e) {
Messaging.logTr(Messages.ERROR_GETTING_FIELD, type, e.getLocalizedMessage());
}
Expand All @@ -242,10 +256,10 @@ public static MethodHandle getFirstFinalSetter(Class<?> clazz, Class<?> type) {

public static MethodHandle getFirstGetter(Class<?> clazz, Class<?> type) {
try {
Field found = getFirstFieldMatchingType(clazz, type, false);
if (found == null)
List<Field> found = getFieldsMatchingType(clazz, type, false);
if (found.isEmpty())
return null;
return LOOKUP.unreflectGetter(found);
return LOOKUP.unreflectGetter(found.get(0));
} catch (Exception e) {
Messaging.logTr(Messages.ERROR_GETTING_FIELD, type, e.getLocalizedMessage());
}
Expand Down Expand Up @@ -292,10 +306,10 @@ public static MethodHandle getFirstMethodHandleWithReturnType(Class<?> clazz, bo

public static MethodHandle getFirstSetter(Class<?> clazz, Class<?> type) {
try {
Field found = getFirstFieldMatchingType(clazz, type, false);
if (found == null)
List<Field> found = getFieldsMatchingType(clazz, type, false);
if (found.isEmpty())
return null;
return LOOKUP.unreflectSetter(found);
return LOOKUP.unreflectSetter(found.get(0));
} catch (Exception e) {
Messaging.logTr(Messages.ERROR_GETTING_FIELD, type, e.getLocalizedMessage());
}
Expand All @@ -304,10 +318,10 @@ public static MethodHandle getFirstSetter(Class<?> clazz, Class<?> type) {

public static MethodHandle getFirstStaticGetter(Class<?> clazz, Class<?> type) {
try {
Field found = getFirstFieldMatchingType(clazz, type, true);
if (found == null)
List<Field> found = getFieldsMatchingType(clazz, type, true);
if (found.isEmpty())
return null;
return LOOKUP.unreflectGetter(found);
return LOOKUP.unreflectGetter(found.get(0));
} catch (Exception e) {
Messaging.logTr(Messages.ERROR_GETTING_FIELD, type, e.getLocalizedMessage());
}
Expand Down Expand Up @@ -698,6 +712,10 @@ public static void setSneaking(Entity entity, boolean sneaking) {
BRIDGE.setSneaking(entity, sneaking);
}

public static void setSnifferState(Entity entity, SnifferState state) {
BRIDGE.setSnifferState(entity, state);
}

public static void setStepHeight(org.bukkit.entity.Entity entity, float height) {
BRIDGE.setStepHeight(entity, height);
}
Expand Down Expand Up @@ -757,6 +775,7 @@ public static void updatePathfindingRange(NPC npc, float pathfindingRange) {
private static Object UNSAFE;
private static MethodHandle UNSAFE_FIELD_OFFSET;
private static MethodHandle UNSAFE_PUT_OBJECT;

private static MethodHandle UNSAFE_STATIC_FIELD_OFFSET;

static {
Expand Down
6 changes: 5 additions & 1 deletion main/src/main/java/net/citizensnpcs/util/NMSBridge.java
Expand Up @@ -40,6 +40,7 @@
import net.citizensnpcs.npc.ai.MCTargetStrategy.TargetNavigator;
import net.citizensnpcs.npc.skin.SkinnableEntity;
import net.citizensnpcs.trait.versioned.CamelTrait.CamelPose;
import net.citizensnpcs.trait.versioned.SnifferTrait.SnifferState;

public interface NMSBridge {
default void activate(Entity entity) {
Expand Down Expand Up @@ -227,6 +228,9 @@ public default void setPolarBearRearing(Entity entity, boolean rearing) {

public void setSneaking(Entity entity, boolean sneaking);

public default void setSnifferState(Entity entity, SnifferState state) {
}

public void setStepHeight(Entity entity, float height);

public void setTeamNameTagVisible(Team team, boolean visible);
Expand All @@ -249,5 +253,5 @@ public default void setPolarBearRearing(Entity entity, boolean rearing) {

public void updateNavigationWorld(Entity entity, World world);

public void updatePathfindingRange(NPC npc, float pathfindingRange);
public void updatePathfindingRange(NPC npc, float pathfindingRange);;
}
1 change: 1 addition & 0 deletions main/src/main/resources/messages_en.properties
Expand Up @@ -280,6 +280,7 @@ citizens.commands.npc.skin.cleared=[[{0}]]''s skin name was cleared.
citizens.commands.npc.skin.layers-set=[[{0}]]''s skin layers: cape - [[{1}]], hat - [[{2}]], jacket - [[{3}]], sleeves - [[{4}]], pants - [[{5}]].
citizens.commands.npc.size.description=[[{0}]]''s size is [[{1}]].
citizens.commands.npc.size.set=[[{0}]]''s size set to [[{1}]].
citizens.commands.npc.sniffer.state-set=[[{0}]]''s state set to [[{1}]].
citizens.commands.npc.spellcaster.spell-set=Spell set to [[{0}]].
citizens.commands.npc.sound.invalid-sound=Invalid sound.
citizens.commands.npc.sound.set=[[{0}]]''s sounds are now: ambient - [[{1}]] hurt - [[{2}]] and death - [[{3}]].
Expand Down

0 comments on commit 299f781

Please sign in to comment.