The dragon breath scroll now works across dimensions

This commit is contained in:
Sollace 2024-12-19 18:19:13 +01:00
parent d5ee43abee
commit ffca3df862
No known key found for this signature in database
GPG key ID: E52FACE7B5C773DB
3 changed files with 20 additions and 10 deletions

View file

@ -285,9 +285,9 @@ public abstract class Living<T extends LivingEntity> implements Equine<T>, Caste
Vec3d targetPos = entity.getRotationVector().multiply(2).add(entity.getEyePos()); Vec3d targetPos = entity.getRotationVector().multiply(2).add(entity.getEyePos());
if (entity.getWorld().isAir(BlockPos.ofFloored(targetPos))) { if (entity.getWorld().isAir(BlockPos.ofFloored(targetPos))) {
DragonBreathStore store = DragonBreathStore.get(entity.getWorld());
String name = entity.getDisplayName().getString(); String name = entity.getDisplayName().getString();
store.popEntries(name).forEach(stack -> {
DragonBreathStore.popAll(entity.getServer(), name).forEach(stack -> {
ItemStack payload = stack.payload(); ItemStack payload = stack.payload();
Item item = payload.getItem(); Item item = payload.getItem();
@ -300,7 +300,7 @@ public abstract class Living<T extends LivingEntity> implements Equine<T>, Caste
ItemStack instance = payload.split(1); ItemStack instance = payload.split(1);
BlockPos pos = BlockPos.ofFloored(randomPos); BlockPos pos = BlockPos.ofFloored(randomPos);
if (!entity.getWorld().isAir(pos)) { if (!entity.getWorld().isAir(pos)) {
store.put(name, instance); stack.store().put(name, instance);
} else { } else {
for (int i = 0; i < 10; i++) { for (int i = 0; i < 10; i++) {
@ -326,7 +326,7 @@ public abstract class Living<T extends LivingEntity> implements Equine<T>, Caste
} while (!payload.isEmpty()); } while (!payload.isEmpty());
} else { } else {
if (!entity.getWorld().isAir(BlockPos.ofFloored(randomPos))) { if (!entity.getWorld().isAir(BlockPos.ofFloored(randomPos))) {
store.put(name, stack.payload()); stack.store().put(name, stack.payload());
} else { } else {
for (int i = 0; i < 10; i++) { for (int i = 0; i < 10; i++) {
ParticleUtils.spawnParticle(entity.getWorld(), ParticleTypes.FLAME, randomPos.add( ParticleUtils.spawnParticle(entity.getWorld(), ParticleTypes.FLAME, randomPos.add(

View file

@ -30,7 +30,7 @@ public class DragonBreathScrollItem extends Item {
return TypedActionResult.fail(stack); return TypedActionResult.fail(stack);
} }
ItemStack scroll = stack.split(1); ItemStack scroll = stack.splitUnlessCreative(1, player);
if (!world.isClient) { if (!world.isClient) {
String recipient = scroll.get(DataComponentTypes.CUSTOM_NAME).getString(); String recipient = scroll.get(DataComponentTypes.CUSTOM_NAME).getString();
UCriteria.SEND_DRAGON_BREATH.triggerSent(player, payload, recipient, (counterName, count) -> { UCriteria.SEND_DRAGON_BREATH.triggerSent(player, payload, recipient, (counterName, count) -> {

View file

@ -1,6 +1,8 @@
package com.minelittlepony.unicopia.server.world; package com.minelittlepony.unicopia.server.world;
import java.util.*; import java.util.*;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;
import com.minelittlepony.unicopia.Unicopia; import com.minelittlepony.unicopia.Unicopia;
import com.minelittlepony.unicopia.item.UItems; import com.minelittlepony.unicopia.item.UItems;
@ -8,6 +10,7 @@ import com.minelittlepony.unicopia.item.UItems;
import net.minecraft.item.ItemStack; import net.minecraft.item.ItemStack;
import net.minecraft.nbt.*; import net.minecraft.nbt.*;
import net.minecraft.registry.RegistryWrapper.WrapperLookup; import net.minecraft.registry.RegistryWrapper.WrapperLookup;
import net.minecraft.server.MinecraftServer;
import net.minecraft.util.Identifier; import net.minecraft.util.Identifier;
import net.minecraft.world.PersistentState; import net.minecraft.world.PersistentState;
import net.minecraft.world.World; import net.minecraft.world.World;
@ -29,7 +32,7 @@ public class DragonBreathStore extends PersistentState {
this(world); this(world);
compound.getKeys().forEach(key -> { compound.getKeys().forEach(key -> {
compound.getList(key, NbtElement.COMPOUND_TYPE).forEach(entry -> { compound.getList(key, NbtElement.COMPOUND_TYPE).forEach(entry -> {
put(key, new Entry((NbtCompound)entry, world.getRegistryManager())); put(key, new Entry((NbtCompound)entry, world.getRegistryManager(), this));
}); });
}); });
} }
@ -55,6 +58,12 @@ public class DragonBreathStore extends PersistentState {
} }
} }
public static Stream<Entry> popAll(MinecraftServer server, String recipient) {
return StreamSupport.stream(server.getWorlds().spliterator(), false)
.map(DragonBreathStore::get)
.flatMap(store -> store.popEntries(recipient).stream());
}
public List<Entry> popEntries(String recipient) { public List<Entry> popEntries(String recipient) {
synchronized (locker) { synchronized (locker) {
List<Entry> entries = doPurge().get(recipient); List<Entry> entries = doPurge().get(recipient);
@ -100,7 +109,7 @@ public class DragonBreathStore extends PersistentState {
} }
return false; return false;
})) { })) {
put(recipient, new Entry(System.currentTimeMillis() + (long)(Math.random() * 1999), payload)); put(recipient, new Entry(System.currentTimeMillis() + (long)(Math.random() * 1999), payload, this));
} }
} }
} }
@ -122,10 +131,11 @@ public class DragonBreathStore extends PersistentState {
public record Entry( public record Entry(
long created, long created,
ItemStack payload) { ItemStack payload,
DragonBreathStore store) {
public Entry(NbtCompound compound, WrapperLookup lookup) { public Entry(NbtCompound compound, WrapperLookup lookup, DragonBreathStore store) {
this(compound.getLong("created"), ItemStack.fromNbtOrEmpty(lookup, compound.getCompound("payload"))); this(compound.getLong("created"), ItemStack.fromNbtOrEmpty(lookup, compound.getCompound("payload")), store);
} }
public NbtCompound toNBT(NbtCompound compound) { public NbtCompound toNBT(NbtCompound compound) {