ignore applying disallowed property

This commit is contained in:
D3v1s0m 2023-10-02 09:52:27 +05:30
parent a5de0680c8
commit 256f47bb94
No known key found for this signature in database
GPG Key ID: FA1F770C7B1D40C1
4 changed files with 12 additions and 3 deletions

@ -37,7 +37,7 @@ public class PropertyRemoveCommand implements CommandHandler {
public List<String> suggest(CommandContext context) throws CommandExecutionException { public List<String> suggest(CommandContext context) throws CommandExecutionException {
if (context.argSize() == 1) return context.suggestCollection(npcRegistry.getModifiableIds()); if (context.argSize() == 1) return context.suggestCollection(npcRegistry.getModifiableIds());
if (context.argSize() == 2) return context.suggestStream(context.suggestionParse(0, NpcEntryImpl.class) if (context.argSize() == 2) return context.suggestStream(context.suggestionParse(0, NpcEntryImpl.class)
.getNpc().getAppliedProperties().stream().filter(EntityProperty::isPlayerModifiable).map(EntityProperty::getName)); .getNpc().getAllProperties().stream().filter(EntityProperty::isPlayerModifiable).map(EntityProperty::getName));
return Collections.emptyList(); return Collections.emptyList();
} }
} }

@ -20,6 +20,7 @@ import org.bukkit.World;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import java.util.*; import java.util.*;
import java.util.stream.Collectors;
public class NpcImpl extends Viewable implements Npc { public class NpcImpl extends Viewable implements Npc {
private final PacketFactory packetFactory; private final PacketFactory packetFactory;
@ -163,9 +164,13 @@ public class NpcImpl extends Viewable implements Npc {
setProperty((EntityPropertyImpl<T>) property, (T) value); setProperty((EntityPropertyImpl<T>) property, (T) value);
} }
public Set<EntityProperty<?>> getAllProperties() {
return Collections.unmodifiableSet(propertyMap.keySet());
}
@Override @Override
public Set<EntityProperty<?>> getAppliedProperties() { public Set<EntityProperty<?>> getAppliedProperties() {
return Collections.unmodifiableSet(propertyMap.keySet()); return Collections.unmodifiableSet(propertyMap.keySet()).stream().filter(type::isAllowedProperty).collect(Collectors.toSet());
} }
public List<InteractionActionImpl> getActions() { public List<InteractionActionImpl> getActions() {

@ -50,6 +50,10 @@ public class NpcTypeImpl implements NpcType {
} }
} }
public boolean isAllowedProperty(EntityPropertyImpl<?> entityProperty) {
return !entityProperty.isPlayerModifiable() || allowedProperties.contains(entityProperty);
}
protected static final class Builder { protected static final class Builder {
private final static Logger logger = Logger.getLogger("NpcTypeBuilder"); private final static Logger logger = Logger.getLogger("NpcTypeBuilder");

@ -111,7 +111,7 @@ public class YamlStorage implements NpcStorage {
config.set("location", serializeLocation(npc.getLocation())); config.set("location", serializeLocation(npc.getLocation()));
config.set("type", npc.getType().getName()); config.set("type", npc.getType().getName());
for (EntityProperty<?> property : npc.getAppliedProperties()) try { for (EntityProperty<?> property : npc.getAllProperties()) try {
PropertySerializer<?> serializer = propertyRegistry.getSerializer(((EntityPropertyImpl<?>) property).getType()); PropertySerializer<?> serializer = propertyRegistry.getSerializer(((EntityPropertyImpl<?>) property).getType());
if (serializer == null) { if (serializer == null) {
Bukkit.getLogger().log(Level.WARNING, "Unknown serializer for property '" + property.getName() + "' for npc '" + entry.getId() + "'. skipping ..."); Bukkit.getLogger().log(Level.WARNING, "Unknown serializer for property '" + property.getName() + "' for npc '" + entry.getId() + "'. skipping ...");