From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: montlikadani <montlikada@gmail.com>
Date: Fri, 13 Nov 2020 17:52:40 +0100
Subject: [PATCH] Add adjustable breeding cooldown to config


diff --git a/src/main/java/net/minecraft/server/EntityAnimal.java b/src/main/java/net/minecraft/server/EntityAnimal.java
index b290218e506d5e4ddd1af17f91de19a588bbcfbd..cf9be4be18e913e49e9150358c66138b64319ed8 100644
--- a/src/main/java/net/minecraft/server/EntityAnimal.java
+++ b/src/main/java/net/minecraft/server/EntityAnimal.java
@@ -121,7 +121,7 @@ public abstract class EntityAnimal extends EntityAgeable {
         if (this.k(itemstack)) {
             int i = this.getAge();
 
-            if (!this.world.isClientSide && i == 0 && this.eP()) {
+            if (!this.world.isClientSide && i == 0 && this.eP() && (this.world.purpurConfig.animalBreedingCooldownSeconds <= 0 || !this.world.hasBreedingCooldown(entityhuman.getUniqueID(), this.getClass()))) { // Purpur
                 this.a(entityhuman, itemstack);
                 this.g(entityhuman);
                 return EnumInteractionResult.SUCCESS;
@@ -213,6 +213,14 @@ public abstract class EntityAnimal extends EntityAgeable {
             if (entityplayer == null && entityanimal.getBreedCause() != null) {
                 entityplayer = entityanimal.getBreedCause();
             }
+            // Purpur start
+            if (entityplayer != null && worldserver.purpurConfig.animalBreedingCooldownSeconds > 0) {
+                if (worldserver.hasBreedingCooldown(entityplayer.getUniqueID(), this.getClass())) {
+                    return;
+                }
+                worldserver.addBreedingCooldown(entityplayer.getUniqueID(), this.getClass());
+            }
+            // Purpur end
             // CraftBukkit start - call EntityBreedEvent
             int experience = this.getRandom().nextInt(7) + 1;
             org.bukkit.event.entity.EntityBreedEvent entityBreedEvent = org.bukkit.craftbukkit.event.CraftEventFactory.callEntityBreedEvent(entityageable, this, entityanimal, entityplayer, this.breedItem, experience);
diff --git a/src/main/java/net/minecraft/server/World.java b/src/main/java/net/minecraft/server/World.java
index d72580deaa50617b5b6a991777f4b36c138308a6..87766db4e3c2e522b738fa304a861dc0985d878a 100644
--- a/src/main/java/net/minecraft/server/World.java
+++ b/src/main/java/net/minecraft/server/World.java
@@ -104,6 +104,48 @@ public abstract class World implements GeneratorAccess, AutoCloseable {
     private int tileTickPosition;
     public final Map<Explosion.CacheKey, Float> explosionDensityCache = new HashMap<>(); // Paper - Optimize explosions
     public java.util.ArrayDeque<BlockRedstoneTorch.RedstoneUpdateInfo> redstoneUpdateInfos; // Paper - Move from Map in BlockRedstoneTorch to here
+    // Purpur start
+    private com.google.common.cache.Cache<BreedingCooldownPair, Object> playerBreedingCooldowns;
+
+    private com.google.common.cache.Cache<BreedingCooldownPair, Object> getNewBreedingCooldownCache() {
+        return com.google.common.cache.CacheBuilder.newBuilder().expireAfterWrite(this.purpurConfig.animalBreedingCooldownSeconds, java.util.concurrent.TimeUnit.SECONDS).build();
+    }
+
+    public void resetBreedingCooldowns() {
+        this.playerBreedingCooldowns = this.getNewBreedingCooldownCache();
+    }
+
+    boolean hasBreedingCooldown(java.util.UUID player, Class<? extends EntityAnimal> animalType) {
+        return this.playerBreedingCooldowns.getIfPresent(new BreedingCooldownPair(player, animalType)) != null;
+    }
+
+    void addBreedingCooldown(java.util.UUID player, Class<? extends EntityAnimal> animalType) {
+        this.playerBreedingCooldowns.put(new BreedingCooldownPair(player, animalType), new Object());
+    }
+
+    private static final class BreedingCooldownPair {
+        private final java.util.UUID playerUUID;
+        private final Class<? extends EntityAnimal> animalType;
+
+        public BreedingCooldownPair(java.util.UUID playerUUID, Class<? extends EntityAnimal> animalType) {
+            this.playerUUID = playerUUID;
+            this.animalType = animalType;
+        }
+
+        @Override
+        public boolean equals(Object o) {
+            if (this == o) return true;
+            if (o == null || getClass() != o.getClass()) return false;
+            BreedingCooldownPair that = (BreedingCooldownPair) o;
+            return playerUUID.equals(that.playerUUID) && animalType.equals(that.animalType);
+        }
+
+        @Override
+        public int hashCode() {
+            return java.util.Objects.hash(playerUUID, animalType);
+        }
+    }
+    // Purpur end
 
     public CraftWorld getWorld() {
         return this.world;
@@ -188,6 +230,7 @@ public abstract class World implements GeneratorAccess, AutoCloseable {
         this.paperConfig = new com.destroystokyo.paper.PaperWorldConfig(((WorldDataServer) worlddatamutable).getName(), this.spigotConfig); // Paper
         this.tuinityConfig = new com.tuinity.tuinity.config.TuinityConfig.WorldConfig(((WorldDataServer)worlddatamutable).getName()); // Tuinity - Server Config
         this.purpurConfig = new net.pl3x.purpur.PurpurWorldConfig(((WorldDataServer) worlddatamutable).getName(), env); // Purpur
+        this.playerBreedingCooldowns = this.getNewBreedingCooldownCache(); // Purpur
         this.chunkPacketBlockController = this.paperConfig.antiXray ? new ChunkPacketBlockControllerAntiXray(this, executor) : ChunkPacketBlockController.NO_OPERATION_INSTANCE; // Paper - Anti-Xray
         this.generator = gen;
         this.world = new CraftWorld((WorldServer) this, gen, env);
diff --git a/src/main/java/net/pl3x/purpur/PurpurWorldConfig.java b/src/main/java/net/pl3x/purpur/PurpurWorldConfig.java
index 9f8d9b65b151149720ce2d69299ddd0bbe0045d1..84b2b8618162f81369dac9e8341374447d9b1737 100644
--- a/src/main/java/net/pl3x/purpur/PurpurWorldConfig.java
+++ b/src/main/java/net/pl3x/purpur/PurpurWorldConfig.java
@@ -220,6 +220,7 @@ public class PurpurWorldConfig {
     public double tridentLoyaltyVoidReturnHeight = 0.0D;
     public double voidDamageHeight = -64.0D;
     public int raidCooldownSeconds = 0;
+    public int animalBreedingCooldownSeconds = 0;
     private void miscGameplayMechanicsSettings() {
         useBetterMending = getBoolean("gameplay-mechanics.use-better-mending", useBetterMending);
         boatEjectPlayersOnLand = getBoolean("gameplay-mechanics.boat.eject-players-on-land", boatEjectPlayersOnLand);
@@ -231,6 +232,7 @@ public class PurpurWorldConfig {
         tridentLoyaltyVoidReturnHeight = getDouble("gameplay-mechanics.trident-loyalty-void-return-height", tridentLoyaltyVoidReturnHeight);
         voidDamageHeight = getDouble("gameplay-mechanics.void-damage-height", voidDamageHeight);
         raidCooldownSeconds = getInt("gameplay-mechanics.raid-cooldown-seconds", raidCooldownSeconds);
+        animalBreedingCooldownSeconds = getInt("gameplay-mechanics.animal-breeding-cooldown-seconds", animalBreedingCooldownSeconds);
     }
 
     public boolean catSpawning;
diff --git a/src/main/java/net/pl3x/purpur/command/PurpurCommand.java b/src/main/java/net/pl3x/purpur/command/PurpurCommand.java
index 4904be939c7a4b1d1583fd7b6232c930b79caba6..860d07cd686e0a6e3eebf2deaf6bcecc1fb9dfd2 100644
--- a/src/main/java/net/pl3x/purpur/command/PurpurCommand.java
+++ b/src/main/java/net/pl3x/purpur/command/PurpurCommand.java
@@ -49,6 +49,7 @@ public class PurpurCommand extends Command {
             PurpurConfig.init((File) console.options.valueOf("purpur-settings"));
             for (WorldServer world : console.getWorlds()) {
                 world.purpurConfig.init();
+                world.resetBreedingCooldowns();
             }
             console.server.reloadCount++;
 
