From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 From: Mariell Hoversholm Date: Wed, 22 Apr 2020 23:29:20 +0200 Subject: [PATCH] Add villager reputation API diff --git a/src/main/java/com/destroystokyo/paper/entity/villager/ReputationConstructor.java b/src/main/java/com/destroystokyo/paper/entity/villager/ReputationConstructor.java new file mode 100644 index 0000000000000000000000000000000000000000..c6072615e95bf51c83b2f728fc3288a7043a89af --- /dev/null +++ b/src/main/java/com/destroystokyo/paper/entity/villager/ReputationConstructor.java @@ -0,0 +1,11 @@ +package com.destroystokyo.paper.entity.villager; +// Must have own package due to package-level constructor. + +import Reputation; + +public final class ReputationConstructor { + // Abuse the package-level constructor. + public static Reputation construct(int[] values) { + return new Reputation(values); + } +} diff --git a/src/main/java/net/minecraft/world/entity/ai/gossip/GossipContainer.java b/src/main/java/net/minecraft/world/entity/ai/gossip/GossipContainer.java index c4ece3ac4863067b12c10772debd1b1454bec5b4..0204f05d989d45c0848f810d1953adf0992ce3c2 100644 --- a/src/main/java/net/minecraft/world/entity/ai/gossip/GossipContainer.java +++ b/src/main/java/net/minecraft/world/entity/ai/gossip/GossipContainer.java @@ -27,7 +27,7 @@ import net.minecraft.core.SerializableUUID; public class GossipContainer { - private final Map gossips = Maps.newHashMap(); + private final Map gossips = Maps.newHashMap(); public Map getReputations() { return this.gossips; } // Paper - add getter for reputations public GossipContainer() {} @@ -142,11 +142,11 @@ public class GossipContainer { return k > type.max ? Math.max(type.max, left) : k; } - static class EntityGossips { + public static class EntityGossips { // Paper - make public private final Object2IntMap entries; - private EntityGossips() { + public EntityGossips() { // Paper - make public - update CraftVillager setReputation on change this.entries = new Object2IntOpenHashMap(); } @@ -200,6 +200,28 @@ public class GossipContainer { public void remove(GossipType gossipType) { this.entries.removeInt(gossipType); } + + // Paper start - Add villager reputation API + private static final com.destroystokyo.paper.entity.villager.ReputationType[] REPUTATION_TYPES = com.destroystokyo.paper.entity.villager.ReputationType.values(); + public com.destroystokyo.paper.entity.villager.Reputation getPaperReputation() { + int[] reputation = new int[REPUTATION_TYPES.length]; + reputation[com.destroystokyo.paper.entity.villager.ReputationType.MAJOR_NEGATIVE.ordinal()] = entries.getOrDefault(GossipType.MAJOR_NEGATIVE, 0); + reputation[com.destroystokyo.paper.entity.villager.ReputationType.MAJOR_POSITIVE.ordinal()] = entries.getOrDefault(GossipType.MAJOR_POSITIVE, 0); + reputation[com.destroystokyo.paper.entity.villager.ReputationType.MINOR_NEGATIVE.ordinal()] = entries.getOrDefault(GossipType.MINOR_NEGATIVE, 0); + reputation[com.destroystokyo.paper.entity.villager.ReputationType.MINOR_POSITIVE.ordinal()] = entries.getOrDefault(GossipType.MINOR_POSITIVE, 0); + reputation[com.destroystokyo.paper.entity.villager.ReputationType.TRADING.ordinal()] = entries.getOrDefault(GossipType.TRADING, 0); + return com.destroystokyo.paper.entity.villager.ReputationConstructor.construct(reputation); + } + + public void assignFromPaperReputation(com.destroystokyo.paper.entity.villager.Reputation rep) { + int val; + if ((val = rep.getReputation(com.destroystokyo.paper.entity.villager.ReputationType.MAJOR_NEGATIVE)) != 0) this.entries.put(GossipType.MAJOR_NEGATIVE, val); + if ((val = rep.getReputation(com.destroystokyo.paper.entity.villager.ReputationType.MAJOR_POSITIVE)) != 0) this.entries.put(GossipType.MAJOR_POSITIVE, val); + if ((val = rep.getReputation(com.destroystokyo.paper.entity.villager.ReputationType.MINOR_NEGATIVE)) != 0) this.entries.put(GossipType.MINOR_NEGATIVE, val); + if ((val = rep.getReputation(com.destroystokyo.paper.entity.villager.ReputationType.MINOR_POSITIVE)) != 0) this.entries.put(GossipType.MINOR_POSITIVE, val); + if ((val = rep.getReputation(com.destroystokyo.paper.entity.villager.ReputationType.TRADING)) != 0) this.entries.put(GossipType.TRADING, val); + } + // Paper end } static class GossipEntry { diff --git a/src/main/java/net/minecraft/world/entity/npc/Villager.java b/src/main/java/net/minecraft/world/entity/npc/Villager.java index a83a7d37f3d769535161fda46fca6f71dcc4d515..e9912551e6a19d6ad3b20fad1b716577b9d28f99 100644 --- a/src/main/java/net/minecraft/world/entity/npc/Villager.java +++ b/src/main/java/net/minecraft/world/entity/npc/Villager.java @@ -1037,6 +1037,7 @@ public class Villager extends AbstractVillager implements ReputationEventHandler this.numberOfRestocksToday = 0; } + public GossipContainer getReputation() { return this.getGossips(); } // Paper - OBFHELPER public GossipContainer getGossips() { return this.gossips; } diff --git a/src/main/java/org/bukkit/craftbukkit/entity/CraftVillager.java b/src/main/java/org/bukkit/craftbukkit/entity/CraftVillager.java index d0b933cfd02b237bfe85011831dab6e8e966496e..e3d4214ef6360b4a9949a73ba3d665ad08733b43 100644 --- a/src/main/java/org/bukkit/craftbukkit/entity/CraftVillager.java +++ b/src/main/java/org/bukkit/craftbukkit/entity/CraftVillager.java @@ -16,6 +16,13 @@ import org.bukkit.entity.Villager; import org.bukkit.entity.Villager.Profession; import org.bukkit.entity.Villager.Type; +// Paper start +import com.destroystokyo.paper.entity.villager.Reputation; +import com.google.common.collect.Maps; +import java.util.Map; +import java.util.UUID; +// Paper end + public class CraftVillager extends CraftAbstractVillager implements Villager { public CraftVillager(CraftServer server, net.minecraft.world.entity.npc.Villager entity) { @@ -125,4 +132,45 @@ public class CraftVillager extends CraftAbstractVillager implements Villager { public static VillagerProfession bukkitToNmsProfession(Profession bukkit) { return Registry.VILLAGER_PROFESSION.get(CraftNamespacedKey.toMinecraft(bukkit.getKey())); } + + // Paper start - Add villager reputation API + @Override + public Reputation getReputation(UUID uniqueId) { + net.minecraft.world.entity.ai.gossip.GossipContainer.EntityGossips rep = getHandle().getReputation().getReputations().get(uniqueId); + if (rep == null) { + return new Reputation(Maps.newHashMap()); + } + + return rep.getPaperReputation(); + } + + @Override + public Map getReputations() { + return getHandle().getReputation().getReputations().entrySet() + .stream() + .collect(java.util.stream.Collectors.toMap(Map.Entry::getKey, entry -> entry.getValue().getPaperReputation())); + } + + @Override + public void setReputation(UUID uniqueId, Reputation reputation) { + net.minecraft.world.entity.ai.gossip.GossipContainer.EntityGossips nmsReputation = + getHandle().getReputation().getReputations().computeIfAbsent( + uniqueId, + key -> new net.minecraft.world.entity.ai.gossip.GossipContainer.EntityGossips() + ); + nmsReputation.assignFromPaperReputation(reputation); + } + + @Override + public void setReputations(Map reputations) { + for (Map.Entry entry : reputations.entrySet()) { + setReputation(entry.getKey(), entry.getValue()); + } + } + + @Override + public void clearReputations() { + getHandle().getReputation().getReputations().clear(); + } + // Paper end }