2021-06-11 14:02:28 +02:00
|
|
|
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
|
|
|
|
From: Mariell Hoversholm <proximyst@proximyst.com>
|
|
|
|
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
|
2021-06-14 07:32:56 +02:00
|
|
|
index 0000000000000000000000000000000000000000..0f10c333d88f2e1c56a6c7f22d421084adfd3789
|
2021-06-11 14:02:28 +02:00
|
|
|
--- /dev/null
|
|
|
|
+++ b/src/main/java/com/destroystokyo/paper/entity/villager/ReputationConstructor.java
|
2021-06-14 07:32:56 +02:00
|
|
|
@@ -0,0 +1,9 @@
|
2021-06-11 14:02:28 +02:00
|
|
|
+package com.destroystokyo.paper.entity.villager;
|
|
|
|
+// Must have own package due to package-level constructor.
|
|
|
|
+
|
|
|
|
+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
|
2022-03-01 06:43:03 +01:00
|
|
|
index 22aa000ad7d44a86231fd8ad93083c972f14caa6..125d08fc4536f15604ef13636056c94f77b5df57 100644
|
2021-06-11 14:02:28 +02:00
|
|
|
--- a/src/main/java/net/minecraft/world/entity/ai/gossip/GossipContainer.java
|
|
|
|
+++ b/src/main/java/net/minecraft/world/entity/ai/gossip/GossipContainer.java
|
2022-03-01 06:43:03 +01:00
|
|
|
@@ -28,7 +28,7 @@ import net.minecraft.util.VisibleForDebug;
|
2021-06-11 14:02:28 +02:00
|
|
|
|
|
|
|
public class GossipContainer {
|
2021-06-14 07:32:56 +02:00
|
|
|
public static final int DISCARD_THRESHOLD = 2;
|
2021-06-11 14:02:28 +02:00
|
|
|
- private final Map<UUID, GossipContainer.EntityGossips> gossips = Maps.newHashMap();
|
|
|
|
+ private final Map<UUID, GossipContainer.EntityGossips> gossips = Maps.newHashMap(); public Map<UUID, GossipContainer.EntityGossips> getReputations() { return this.gossips; } // Paper - add getter for reputations
|
|
|
|
|
2021-06-14 07:32:56 +02:00
|
|
|
@VisibleForDebug
|
|
|
|
public Map<UUID, Object2IntMap<GossipType>> getGossipEntries() {
|
2022-03-01 06:43:03 +01:00
|
|
|
@@ -227,6 +227,28 @@ public class GossipContainer {
|
2021-06-11 14:02:28 +02:00
|
|
|
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/org/bukkit/craftbukkit/entity/CraftVillager.java b/src/main/java/org/bukkit/craftbukkit/entity/CraftVillager.java
|
2021-12-03 06:46:07 +01:00
|
|
|
index 0cd83a20ab565c9a5a38f19eed016289237e72ab..dbc1ea96223675fbe03585598a9c7f51acc61d2e 100644
|
2021-06-11 14:02:28 +02:00
|
|
|
--- 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) {
|
2021-12-03 06:46:07 +01:00
|
|
|
@@ -139,4 +146,45 @@ public class CraftVillager extends CraftAbstractVillager implements Villager {
|
2021-06-11 14:02:28 +02:00
|
|
|
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) {
|
2021-06-17 22:20:03 +02:00
|
|
|
+ net.minecraft.world.entity.ai.gossip.GossipContainer.EntityGossips rep = getHandle().getGossips().getReputations().get(uniqueId);
|
2021-06-11 14:02:28 +02:00
|
|
|
+ if (rep == null) {
|
|
|
|
+ return new Reputation(Maps.newHashMap());
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ return rep.getPaperReputation();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public Map<UUID, Reputation> getReputations() {
|
2021-06-17 22:20:03 +02:00
|
|
|
+ return getHandle().getGossips().getReputations().entrySet()
|
2021-06-11 14:02:28 +02:00
|
|
|
+ .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 =
|
2021-06-17 22:20:03 +02:00
|
|
|
+ getHandle().getGossips().getReputations().computeIfAbsent(
|
2021-06-11 14:02:28 +02:00
|
|
|
+ uniqueId,
|
|
|
|
+ key -> new net.minecraft.world.entity.ai.gossip.GossipContainer.EntityGossips()
|
|
|
|
+ );
|
|
|
|
+ nmsReputation.assignFromPaperReputation(reputation);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public void setReputations(Map<UUID, Reputation> reputations) {
|
|
|
|
+ for (Map.Entry<UUID, Reputation> entry : reputations.entrySet()) {
|
|
|
|
+ setReputation(entry.getKey(), entry.getValue());
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public void clearReputations() {
|
2021-06-17 22:20:03 +02:00
|
|
|
+ getHandle().getGossips().getReputations().clear();
|
2021-06-11 14:02:28 +02:00
|
|
|
+ }
|
|
|
|
+ // Paper end
|
|
|
|
}
|