3
0
Mirror von https://github.com/PaperMC/Paper.git synchronisiert 2024-11-15 20:40:07 +01:00
Paper/Spigot-Server-Patches/0476-Add-villager-reputation-API.patch
Daniel Ennis c97ce029e9
1.16.2 Release (#4123)
PaperMC believes that 1.16.2 is now ready for general release as we fixed the main issue plagueing the 1.16.x release, the MapLike data conversion issues.

Until now, it was not safe for a server to convert a world to 1.16.2 without data conversion issues around villages and potentially other things. If you did, those MapLike errors meant something went wrong.

This is now resolved.

Big thanks to all those that helped, notably @BillyGalbreath and @Proximyst who did large parts of the update process with me.

Please as always, backup your worlds and test before updating to 1.16.2!

If you update to 1.16.2, there is no going back to an older build than this.

---------------------------------

Co-authored-by: William Blake Galbreath <Blake.Galbreath@GMail.com>
Co-authored-by: Mariell Hoversholm <proximyst@proximyst.com>
Co-authored-by: krolik-exe <69214078+krolik-exe@users.noreply.github.com>
Co-authored-by: BillyGalbreath <BillyGalbreath@users.noreply.github.com>
Co-authored-by: stonar96 <minecraft.stonar96@gmail.com>
Co-authored-by: Shane Freeder <theboyetronic@gmail.com>
Co-authored-by: Jason <jasonpenilla2@me.com>
Co-authored-by: kashike <kashike@vq.lc>
Co-authored-by: Aurora <21148213+aurorasmiles@users.noreply.github.com>
Co-authored-by: KennyTV <kennytv@t-online.de>
Co-authored-by: commandblockguy <commandblockguy1@gmail.com>
Co-authored-by: DigitalRegent <misterwener@gmail.com>
Co-authored-by: ishland <ishlandmc@yeah.net>
2020-08-24 22:40:19 -04:00

154 Zeilen
7.6 KiB
Diff

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
index 0000000000000000000000000000000000000000..0f10c333d88f2e1c56a6c7f22d421084adfd3789
--- /dev/null
+++ b/src/main/java/com/destroystokyo/paper/entity/villager/ReputationConstructor.java
@@ -0,0 +1,9 @@
+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/server/EntityVillager.java b/src/main/java/net/minecraft/server/EntityVillager.java
index 70f15bc9de4d66da7991a18b438737db18e1e76e..c10ff09dd005c0c8b3cd374d9de82e77efcd9097 100644
--- a/src/main/java/net/minecraft/server/EntityVillager.java
+++ b/src/main/java/net/minecraft/server/EntityVillager.java
@@ -947,6 +947,7 @@ public class EntityVillager extends EntityVillagerAbstract implements Reputation
this.bD = 0;
}
+ public Reputation getReputation() { return this.fj(); } // Paper - OBFHELPER
public Reputation fj() {
return this.by;
}
diff --git a/src/main/java/net/minecraft/server/Reputation.java b/src/main/java/net/minecraft/server/Reputation.java
index 3c6d6be4485cad4f2e9a395425b5837590853eee..09d2fc5769089f6d29971d10de5b8209829baae8 100644
--- a/src/main/java/net/minecraft/server/Reputation.java
+++ b/src/main/java/net/minecraft/server/Reputation.java
@@ -25,7 +25,7 @@ import java.util.stream.Stream;
public class Reputation {
- private final Map<UUID, Reputation.a> a = Maps.newHashMap();
+ private final Map<UUID, Reputation.a> a = Maps.newHashMap(); public Map<UUID, Reputation.a> getReputations() { return this.a; } // Paper - add getter for reputations
public Reputation() {}
@@ -140,11 +140,11 @@ public class Reputation {
return k > reputationtype.h ? Math.max(reputationtype.h, i) : k;
}
- static class a {
+ public static class a { // Paper - make public
private final Object2IntMap<ReputationType> a;
- private a() {
+ public a() { // Paper - make public - update CraftVillager setReputation on change
this.a = new Object2IntOpenHashMap();
}
@@ -198,6 +198,28 @@ public class Reputation {
public void b(ReputationType reputationtype) {
this.a.removeInt(reputationtype);
}
+
+ // 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()] = a.getOrDefault(ReputationType.MAJOR_NEGATIVE, 0);
+ reputation[com.destroystokyo.paper.entity.villager.ReputationType.MAJOR_POSITIVE.ordinal()] = a.getOrDefault(ReputationType.MAJOR_POSITIVE, 0);
+ reputation[com.destroystokyo.paper.entity.villager.ReputationType.MINOR_NEGATIVE.ordinal()] = a.getOrDefault(ReputationType.MINOR_NEGATIVE, 0);
+ reputation[com.destroystokyo.paper.entity.villager.ReputationType.MINOR_POSITIVE.ordinal()] = a.getOrDefault(ReputationType.MINOR_POSITIVE, 0);
+ reputation[com.destroystokyo.paper.entity.villager.ReputationType.TRADING.ordinal()] = a.getOrDefault(ReputationType.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.a.put(ReputationType.MAJOR_NEGATIVE, val);
+ if ((val = rep.getReputation(com.destroystokyo.paper.entity.villager.ReputationType.MAJOR_POSITIVE)) != 0) this.a.put(ReputationType.MAJOR_POSITIVE, val);
+ if ((val = rep.getReputation(com.destroystokyo.paper.entity.villager.ReputationType.MINOR_NEGATIVE)) != 0) this.a.put(ReputationType.MINOR_NEGATIVE, val);
+ if ((val = rep.getReputation(com.destroystokyo.paper.entity.villager.ReputationType.MINOR_POSITIVE)) != 0) this.a.put(ReputationType.MINOR_POSITIVE, val);
+ if ((val = rep.getReputation(com.destroystokyo.paper.entity.villager.ReputationType.TRADING)) != 0) this.a.put(ReputationType.TRADING, val);
+ }
+ // Paper end
}
static class b {
diff --git a/src/main/java/org/bukkit/craftbukkit/entity/CraftVillager.java b/src/main/java/org/bukkit/craftbukkit/entity/CraftVillager.java
index a8384081c03884c86578dca677914d77441c1863..d24a892c498d7ee58741c9358748a117f01d8a8d 100644
--- a/src/main/java/org/bukkit/craftbukkit/entity/CraftVillager.java
+++ b/src/main/java/org/bukkit/craftbukkit/entity/CraftVillager.java
@@ -1,9 +1,13 @@
package org.bukkit.craftbukkit.entity;
+import com.destroystokyo.paper.entity.villager.Reputation; // Paper
import com.google.common.base.Preconditions;
+import com.google.common.collect.Maps; // Paper
import java.util.Locale;
import net.minecraft.server.BlockBed;
import net.minecraft.server.BlockPosition;
+import java.util.Map; // Paper
+import java.util.UUID; // Paper
import net.minecraft.server.EntityVillager;
import net.minecraft.server.IBlockData;
import net.minecraft.server.IRegistry;
@@ -124,4 +128,45 @@ public class CraftVillager extends CraftAbstractVillager implements Villager {
public static VillagerProfession bukkitToNmsProfession(Profession bukkit) {
return IRegistry.VILLAGER_PROFESSION.get(CraftNamespacedKey.toMinecraft(bukkit.getKey()));
}
+
+ // Paper start - Add villager reputation API
+ @Override
+ public Reputation getReputation(UUID uniqueId) {
+ net.minecraft.server.Reputation.a rep = getHandle().getReputation().getReputations().get(uniqueId);
+ if (rep == null) {
+ return new Reputation(Maps.newHashMap());
+ }
+
+ return rep.getPaperReputation();
+ }
+
+ @Override
+ public Map<UUID, Reputation> 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.server.Reputation.a nmsReputation =
+ getHandle().getReputation().getReputations().computeIfAbsent(
+ uniqueId,
+ key -> new net.minecraft.server.Reputation.a()
+ );
+ 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() {
+ getHandle().getReputation().getReputations().clear();
+ }
+ // Paper end
}