2022-03-01 06:43:03 +01:00
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Jake Potrebic <jake.m.potrebic@gmail.com>
Date: Sun, 3 Jan 2021 20:03:35 -0800
Subject: [PATCH] Add GameEvent tags
diff --git a/src/main/java/io/papermc/paper/CraftGameEventTag.java b/src/main/java/io/papermc/paper/CraftGameEventTag.java
new file mode 100644
2022-04-03 03:10:13 +02:00
index 0000000000000000000000000000000000000000..cb78a3d4e21376ea24347187478525d5f0c24079
2022-03-01 06:43:03 +01:00
--- /dev/null
+++ b/src/main/java/io/papermc/paper/CraftGameEventTag.java
2022-04-03 03:10:13 +02:00
@@ -0,0 +1,34 @@
2022-03-01 06:43:03 +01:00
+package io.papermc.paper;
+
+import net.minecraft.core.Registry;
+import net.minecraft.resources.ResourceKey;
+import net.minecraft.tags.TagKey;
+import org.bukkit.GameEvent;
+import org.bukkit.craftbukkit.tag.CraftTag;
+import org.bukkit.craftbukkit.util.CraftNamespacedKey;
+import org.jetbrains.annotations.NotNull;
+
2022-04-03 03:10:13 +02:00
+import java.util.Collections;
+import java.util.IdentityHashMap;
+import java.util.Map;
2022-03-01 06:43:03 +01:00
+import java.util.Objects;
+import java.util.Set;
+import java.util.stream.Collectors;
+
+public class CraftGameEventTag extends CraftTag<net.minecraft.world.level.gameevent.GameEvent, GameEvent> {
+
+ public CraftGameEventTag(net.minecraft.core.Registry<net.minecraft.world.level.gameevent.GameEvent> registry, TagKey<net.minecraft.world.level.gameevent.GameEvent> tag) {
+ super(registry, tag);
+ }
+
2022-04-03 03:10:13 +02:00
+ private static final Map<GameEvent, ResourceKey<net.minecraft.world.level.gameevent.GameEvent>> KEY_CACHE = Collections.synchronizedMap(new IdentityHashMap<>());
2022-03-01 06:43:03 +01:00
+ @Override
2022-04-03 03:10:13 +02:00
+ public boolean isTagged(@NotNull GameEvent gameEvent) {
+ return registry.getHolderOrThrow(KEY_CACHE.computeIfAbsent(gameEvent, event -> ResourceKey.create(Registry.GAME_EVENT_REGISTRY, CraftNamespacedKey.toMinecraft(event.getKey())))).is(tag);
2022-03-01 06:43:03 +01:00
+ }
+
+ @Override
+ public @NotNull Set<GameEvent> getValues() {
+ return getHandle().stream().map((nms) -> Objects.requireNonNull(GameEvent.getByKey(CraftNamespacedKey.fromMinecraft(Registry.GAME_EVENT.getKey(nms.value()))), nms + " is not a recognized game event")).collect(Collectors.toUnmodifiableSet());
+ }
+}
diff --git a/src/main/java/org/bukkit/craftbukkit/CraftServer.java b/src/main/java/org/bukkit/craftbukkit/CraftServer.java
2022-06-03 06:26:56 +02:00
index 6ce8e229eabcc021a01c1c29c39091858b598e39..aea07b7b9cbee6198f69fabd32e901d5952687a0 100644
2022-03-01 06:43:03 +01:00
--- a/src/main/java/org/bukkit/craftbukkit/CraftServer.java
+++ b/src/main/java/org/bukkit/craftbukkit/CraftServer.java
@@ -97,6 +97,7 @@ import net.minecraft.world.level.biome.BiomeSource;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.dimension.DimensionType;
import net.minecraft.world.level.dimension.LevelStem;
+import net.minecraft.world.level.gameevent.GameEvent;
import net.minecraft.world.level.levelgen.NoiseBasedChunkGenerator;
import net.minecraft.world.level.levelgen.PatrolSpawner;
import net.minecraft.world.level.levelgen.PhantomSpawner;
2022-05-26 19:59:53 +02:00
@@ -2563,6 +2564,15 @@ public final class CraftServer implements Server {
2022-03-06 09:08:26 +01:00
return (org.bukkit.Tag<T>) new CraftEntityTag(Registry.ENTITY_TYPE, entityTagKey);
}
}
2022-03-01 06:43:03 +01:00
+ // Paper start
2022-03-06 09:08:26 +01:00
+ case org.bukkit.Tag.REGISTRY_GAME_EVENTS -> {
2022-03-01 06:43:03 +01:00
+ Preconditions.checkArgument(clazz == org.bukkit.GameEvent.class, "Game Event namespace must have GameEvent type");
2022-03-06 09:08:26 +01:00
+ TagKey<GameEvent> gameEventTagKey = TagKey.create(Registry.GAME_EVENT_REGISTRY, key);
+ if (Registry.GAME_EVENT.isKnownTagName(gameEventTagKey)) {
+ return (org.bukkit.Tag<T>) new io.papermc.paper.CraftGameEventTag(Registry.GAME_EVENT, gameEventTagKey);
+ }
+ }
2022-03-01 06:43:03 +01:00
+ // Paper end
2022-03-06 09:08:26 +01:00
default -> throw new IllegalArgumentException();
2022-03-01 06:43:03 +01:00
}
2022-05-26 19:59:53 +02:00
@@ -2595,6 +2605,13 @@ public final class CraftServer implements Server {
2022-03-01 06:43:03 +01:00
Registry<EntityType<?>> entityTags = Registry.ENTITY_TYPE;
return entityTags.getTags().map(pair -> (org.bukkit.Tag<T>) new CraftEntityTag(entityTags, pair.getFirst())).collect(ImmutableList.toImmutableList());
2022-03-06 09:08:26 +01:00
}
2022-03-01 06:43:03 +01:00
+ // Paper start
2022-03-06 09:08:26 +01:00
+ case org.bukkit.Tag.REGISTRY_GAME_EVENTS -> {
2022-03-01 06:43:03 +01:00
+ Preconditions.checkArgument(clazz == org.bukkit.GameEvent.class);
+ Registry<GameEvent> gameEvents = Registry.GAME_EVENT;
+ return gameEvents.getTags().map(pair -> (org.bukkit.Tag<T>) new io.papermc.paper.CraftGameEventTag(gameEvents, pair.getFirst())).collect(ImmutableList.toImmutableList());
+ // Paper end
2022-03-06 09:08:26 +01:00
+ }
default -> throw new IllegalArgumentException();
2022-03-01 06:43:03 +01:00
}
2022-03-06 09:08:26 +01:00
}