mirror of
https://github.com/BX-Team/DivineMC.git
synced 2025-12-22 08:19:19 +00:00
212 lines
11 KiB
Diff
212 lines
11 KiB
Diff
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
|
|
From: NONPLAYT <76615486+NONPLAYT@users.noreply.github.com>
|
|
Date: Sat, 1 Feb 2025 16:47:09 +0300
|
|
Subject: [PATCH] Clump experience orbs
|
|
|
|
|
|
diff --git a/net/minecraft/world/entity/ExperienceOrb.java b/net/minecraft/world/entity/ExperienceOrb.java
|
|
index a43e5190c0f9ae14ccecccd5b58dc0e17f18b0a1..06ffba13f211851e8f6d630a72b41474673e8df8 100644
|
|
--- a/net/minecraft/world/entity/ExperienceOrb.java
|
|
+++ b/net/minecraft/world/entity/ExperienceOrb.java
|
|
@@ -49,6 +49,10 @@ public class ExperienceOrb extends Entity {
|
|
@javax.annotation.Nullable
|
|
public java.util.UUID triggerEntityId;
|
|
public org.bukkit.entity.ExperienceOrb.SpawnReason spawnReason = org.bukkit.entity.ExperienceOrb.SpawnReason.UNKNOWN;
|
|
+ // DivineMC start - Clump experience orbs
|
|
+ public java.util.Map<Integer, Integer> clumps$clumpedMap;
|
|
+ public Optional<EnchantedItemInUse> clumps$currentEntry;
|
|
+ // DivineMC end - Clump experience orbs
|
|
|
|
private void loadPaperNBT(CompoundTag tag) {
|
|
if (!tag.contains("Paper.ExpData", net.minecraft.nbt.Tag.TAG_COMPOUND)) {
|
|
@@ -239,6 +243,28 @@ public class ExperienceOrb extends Entity {
|
|
}
|
|
|
|
private static boolean tryMergeToExisting(ServerLevel level, Vec3 pos, int amount) {
|
|
+ // DivineMC start - Clump experience orbs
|
|
+ if (org.bxteam.divinemc.DivineConfig.clumpOrbs) {
|
|
+ AABB aABB = AABB.ofSize(pos, 1.0D, 1.0D, 1.0D);
|
|
+ int id = level.getRandom().nextInt(40);
|
|
+ List<ExperienceOrb> list = level.getEntities(EntityTypeTest.forClass(ExperienceOrb.class), aABB, (experienceOrbx) -> canMerge(experienceOrbx, id, amount));
|
|
+ if(!list.isEmpty()) {
|
|
+ ExperienceOrb experienceOrb = list.getFirst();
|
|
+ java.util.Map<Integer, Integer> clumpedMap = (experienceOrb).clumps$getClumpedMap();
|
|
+ (experienceOrb).clumps$setClumpedMap(java.util.stream.Stream.of(clumpedMap, java.util.Collections.singletonMap(amount, 1))
|
|
+ .flatMap(map -> map.entrySet().stream())
|
|
+ .collect(java.util.stream.Collectors.toMap(java.util.Map.Entry::getKey, java.util.Map.Entry::getValue, Integer::sum)));
|
|
+ (experienceOrb).count = (clumpedMap.values()
|
|
+ .stream()
|
|
+ .reduce(Integer::sum)
|
|
+ .orElse(1));
|
|
+ (experienceOrb).age = (0);
|
|
+ return true;
|
|
+ } else {
|
|
+ return false;
|
|
+ }
|
|
+ }
|
|
+ // DivineMC end - Clump experience orbs
|
|
// Paper - TODO some other event for this kind of merge
|
|
AABB aabb = AABB.ofSize(pos, 1.0, 1.0, 1.0);
|
|
int randomInt = level.getRandom().nextInt(40);
|
|
@@ -254,11 +280,11 @@ public class ExperienceOrb extends Entity {
|
|
}
|
|
|
|
private boolean canMerge(ExperienceOrb orb) {
|
|
- return orb != this && canMerge(orb, this.getId(), this.value);
|
|
+ return org.bxteam.divinemc.DivineConfig.clumpOrbs ? orb.isAlive() && !this.is(orb) : orb != this && ExperienceOrb.canMerge(orb, this.getId(), this.value); // DivineMC - Clump experience orbs
|
|
}
|
|
|
|
private static boolean canMerge(ExperienceOrb orb, int amount, int other) {
|
|
- return !orb.isRemoved() && (orb.getId() - amount) % 40 == 0 && orb.value == other;
|
|
+ return org.bxteam.divinemc.DivineConfig.clumpOrbs ? orb.isAlive() : !orb.isRemoved() && (orb.getId() - amount) % 40 == 0 && orb.value == other; // DivineMC - Clump experience orbs
|
|
}
|
|
|
|
private void merge(ExperienceOrb orb) {
|
|
@@ -267,6 +293,18 @@ public class ExperienceOrb extends Entity {
|
|
return;
|
|
}
|
|
// Paper end - call orb merge event
|
|
+ // DivineMC start - Clump experience orbs
|
|
+ if (org.bxteam.divinemc.DivineConfig.clumpOrbs) {
|
|
+ java.util.Map<Integer, Integer> otherMap = (orb).clumps$getClumpedMap();
|
|
+ this.count = clumps$getClumpedMap().values().stream().reduce(Integer::sum).orElse(1);
|
|
+ this.age = Math.min(this.age, (orb).age);
|
|
+ clumps$setClumpedMap(java.util.stream.Stream.of(clumps$getClumpedMap(), otherMap)
|
|
+ .flatMap(map -> map.entrySet().stream())
|
|
+ .collect(java.util.stream.Collectors.toMap(java.util.Map.Entry::getKey, java.util.Map.Entry::getValue, Integer::sum)));
|
|
+ orb.discard(EntityRemoveEvent.Cause.MERGE); // DivineMC - add Bukkit remove cause
|
|
+ return;
|
|
+ }
|
|
+ // DivineMC end - Clump experience orbs
|
|
this.count = this.count + orb.count;
|
|
this.age = Math.min(this.age, orb.age);
|
|
orb.discard(EntityRemoveEvent.Cause.MERGE); // CraftBukkit - add Bukkit remove cause
|
|
@@ -308,6 +346,13 @@ public class ExperienceOrb extends Entity {
|
|
compound.putInt("Value", this.value); // Paper - save as Integer
|
|
compound.putInt("Count", this.count);
|
|
this.savePaperNBT(compound); // Paper
|
|
+ // DivineMC start - Clump experience orbs
|
|
+ if (clumps$clumpedMap != null) {
|
|
+ CompoundTag map = new CompoundTag();
|
|
+ clumps$getClumpedMap().forEach((value, count) -> map.putInt(String.valueOf(value), count));
|
|
+ compound.put("clumpedMap", map);
|
|
+ }
|
|
+ // DivineMC end - Clump experience orbs
|
|
}
|
|
|
|
@Override
|
|
@@ -317,10 +362,51 @@ public class ExperienceOrb extends Entity {
|
|
this.value = compound.getInt("Value"); // Paper - load as Integer
|
|
this.count = Math.max(compound.getInt("Count"), 1);
|
|
this.loadPaperNBT(compound); // Paper
|
|
+ // DivineMC start - Clump experience orbs
|
|
+ java.util.Map<Integer, Integer> map = new java.util.HashMap<>();
|
|
+ if (compound.contains("clumpedMap")) {
|
|
+ CompoundTag clumpedMap = compound.getCompound("clumpedMap");
|
|
+ for (String s : clumpedMap.getAllKeys()) {
|
|
+ map.put(Integer.parseInt(s), clumpedMap.getInt(s));
|
|
+ }
|
|
+ } else {
|
|
+ map.put(value, count);
|
|
+ }
|
|
+
|
|
+ clumps$setClumpedMap(map);
|
|
+ // DivineMC end - Clump experience orbs
|
|
}
|
|
|
|
@Override
|
|
public void playerTouch(Player entity) {
|
|
+ // DivineMC start - Clump experience orbs
|
|
+ if(entity instanceof ServerPlayer && org.bxteam.divinemc.DivineConfig.clumpOrbs) {
|
|
+ entity.takeXpDelay = 0;
|
|
+ entity.take(this, 1);
|
|
+
|
|
+ if(this.value != 0 || clumps$resolve()) {
|
|
+ java.util.concurrent.atomic.AtomicInteger toGive = new java.util.concurrent.atomic.AtomicInteger();
|
|
+ clumps$getClumpedMap().forEach((value, amount) -> {
|
|
+ int actualValue = value;
|
|
+
|
|
+ for(int i = 0; i < amount; i++) {
|
|
+ int leftOver = actualValue;
|
|
+ if(leftOver == actualValue) {
|
|
+ leftOver = this.repairPlayerItems((ServerPlayer) entity, actualValue);
|
|
+ }
|
|
+ if(leftOver > 0) {
|
|
+ toGive.addAndGet(leftOver);
|
|
+ }
|
|
+ }
|
|
+ });
|
|
+ if(toGive.get() > 0) {
|
|
+ entity.giveExperiencePoints(toGive.get());
|
|
+ }
|
|
+ }
|
|
+ this.discard();
|
|
+ return;
|
|
+ }
|
|
+ // DivineMC end - Clump experience orbs
|
|
if (entity instanceof ServerPlayer serverPlayer) {
|
|
if (entity.takeXpDelay == 0 && new com.destroystokyo.paper.event.player.PlayerPickupExperienceEvent(serverPlayer.getBukkitEntity(), (org.bukkit.entity.ExperienceOrb) this.getBukkitEntity()).callEvent()) { // Paper - PlayerPickupExperienceEvent
|
|
entity.takeXpDelay = CraftEventFactory.callPlayerXpCooldownEvent(entity, this.level().purpurConfig.playerExpPickupDelay, PlayerExpCooldownChangeEvent.ChangeReason.PICKUP_ORB).getNewCooldown(); // CraftBukkit - entityhuman.takeXpDelay = 2; // Purpur - Configurable player pickup exp delay
|
|
@@ -338,10 +424,57 @@ public class ExperienceOrb extends Entity {
|
|
}
|
|
}
|
|
|
|
- private int repairPlayerItems(ServerPlayer player, int value) {
|
|
- Optional<EnchantedItemInUse> randomItemWith = level().purpurConfig.useBetterMending ? EnchantmentHelper.getMostDamagedItemWith(EnchantmentEffectComponents.REPAIR_WITH_XP, player) : EnchantmentHelper.getRandomItemWith( // Purpur - Add option to mend the most damaged equipment first
|
|
- EnchantmentEffectComponents.REPAIR_WITH_XP, player, ItemStack::isDamaged
|
|
- );
|
|
+ // DivineMC start - Clump experience orbs
|
|
+ public Optional<EnchantedItemInUse> clumps$captureCurrentEntry(Optional<EnchantedItemInUse> entry) {
|
|
+ clumps$currentEntry = entry;
|
|
+ return entry;
|
|
+ }
|
|
+
|
|
+ public java.util.Map<Integer, Integer> clumps$getClumpedMap() {
|
|
+ if (clumps$clumpedMap == null) {
|
|
+ clumps$clumpedMap = new java.util.HashMap<>();
|
|
+ clumps$clumpedMap.put(this.value, 1);
|
|
+ }
|
|
+
|
|
+ return clumps$clumpedMap;
|
|
+ }
|
|
+
|
|
+ public void clumps$setClumpedMap(java.util.Map<Integer, Integer> map) {
|
|
+ clumps$clumpedMap = map;
|
|
+ clumps$resolve();
|
|
+ }
|
|
+
|
|
+ public boolean clumps$resolve() {
|
|
+ value = clumps$getClumpedMap().entrySet()
|
|
+ .stream()
|
|
+ .map(entry -> entry.getKey() * entry.getValue())
|
|
+ .reduce(Integer::sum)
|
|
+ .orElse(1);
|
|
+
|
|
+ return value > 0;
|
|
+ }
|
|
+
|
|
+ private int repairPlayerItems(ServerPlayer player, int amount) {
|
|
+ Optional<EnchantedItemInUse> randomItemWith = clumps$captureCurrentEntry(level().purpurConfig.useBetterMending ? EnchantmentHelper.getMostDamagedItemWith(EnchantmentEffectComponents.REPAIR_WITH_XP, player) : EnchantmentHelper.getRandomItemWith(EnchantmentEffectComponents.REPAIR_WITH_XP, player, ItemStack::isDamaged)); // Purpur - Add option to mend the most damaged equipment first
|
|
+
|
|
+ if (org.bxteam.divinemc.DivineConfig.clumpOrbs) {
|
|
+ return clumps$currentEntry
|
|
+ .map(foundItem -> {
|
|
+ ItemStack itemstack = foundItem.itemStack();
|
|
+ int xpToRepair = EnchantmentHelper.modifyDurabilityToRepairFromXp(player.serverLevel(), itemstack, (int) (amount * 1));
|
|
+ int toRepair = Math.min(xpToRepair, itemstack.getDamageValue());
|
|
+ itemstack.setDamageValue(itemstack.getDamageValue() - toRepair);
|
|
+ if(toRepair > 0) {
|
|
+ int used = amount - toRepair * amount / xpToRepair;
|
|
+ if(used > 0) {
|
|
+ return this.repairPlayerItems(player, used);
|
|
+ }
|
|
+ }
|
|
+ return 0;
|
|
+ })
|
|
+ .orElse(amount);
|
|
+ }
|
|
+ // DivineMC end - Clump experience orbs
|
|
if (randomItemWith.isPresent()) {
|
|
ItemStack itemStack = randomItemWith.get().itemStack();
|
|
int i = EnchantmentHelper.modifyDurabilityToRepairFromXp(player.serverLevel(), itemStack, value);
|