feat: hacks for 26.1.2

This commit is contained in:
ArikSquad committed 2026-05-05 19:03:02 +03:00
1 parent 7b44ea93ba
commit 148b3cf6f0
5 files changed
+93 -20

No files matched your search

@@ -1,18 +1,29 @@
package com.dfsek.terra.bukkit.nms;
import com.mojang.brigadier.StringReader;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import net.minecraft.commands.arguments.blocks.BlockStateParser;
import net.minecraft.commands.arguments.blocks.BlockStateParser.BlockResult;
import net.minecraft.core.Holder;
import net.minecraft.core.Holder.Reference;
import net.minecraft.core.HolderSet;
import net.minecraft.core.HolderLookup;
import net.minecraft.core.HolderSet.Named;
import net.minecraft.core.MappedRegistry;
import net.minecraft.core.RegistrationInfo;
import net.minecraft.core.registries.Registries;
import net.minecraft.resources.Identifier;
import net.minecraft.resources.ResourceKey;
import net.minecraft.server.MinecraftServer;
import net.minecraft.tags.TagKey;
import net.minecraft.world.entity.npc.villager.VillagerType;
import net.minecraft.world.level.biome.Biome;
import net.minecraft.world.level.block.Block;
import org.bukkit.Bukkit;
import org.bukkit.NamespacedKey;
import org.bukkit.block.data.BlockData;
import org.bukkit.craftbukkit.CraftServer;
import org.bukkit.craftbukkit.block.data.CraftBlockData;
import org.jetbrains.annotations.NotNull;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -120,7 +131,7 @@ public class AwfulBukkitHacks {
}
private static <T> void bindTags(MappedRegistry<T> registry, Map<TagKey<T>, List<Holder<T>>> tagEntries) {
Map<Holder.Reference<T>, List<TagKey<T>>> map = new IdentityHashMap<>();
Map<Reference<T>, List<TagKey<T>>> map = new IdentityHashMap<>();
Reflection.MAPPED_REGISTRY.getByKey(registry).values().forEach(entry -> map.put(entry, new ArrayList<>()));
tagEntries.forEach((tag, entries) -> {
for(Holder<T> holder : entries) {
@@ -145,7 +156,7 @@ public class AwfulBukkitHacks {
// );
// }
Map<TagKey<T>, HolderSet.Named<T>> map2 = new IdentityHashMap<>(registry.getTags().collect(Collectors.toMap(
Map<TagKey<T>, Named<T>> map2 = new IdentityHashMap<>(registry.getTags().collect(Collectors.toMap(
Named::key,
(named) -> named
)));
@@ -160,5 +171,14 @@ public class AwfulBukkitHacks {
Reflection.MAPPED_REGISTRY.getByKey(registry).values().forEach(
entry -> Reflection.HOLDER_REFERENCE.invokeBindTags(entry, Set.of()));
}
// used by BukkitWorldHandle
public static BlockData createBlockState(@NotNull String data) throws CommandSyntaxException {
MinecraftServer server = ((CraftServer) Bukkit.getServer()).getServer();
HolderLookup.Provider lookup = server.registryAccess();
final HolderLookup<Block> blocks = lookup.lookupOrThrow(Registries.BLOCK);
BlockResult result = BlockStateParser.parseForBlock(blocks, new StringReader(data), true);
return CraftBlockData.createData(result.blockState());
}
}
@@ -11,11 +11,13 @@ import net.minecraft.world.attribute.AmbientMoodSettings;
import net.minecraft.world.attribute.AmbientParticle;
import net.minecraft.world.attribute.AmbientSounds;
import net.minecraft.world.attribute.BackgroundMusic;
import net.minecraft.world.attribute.EnvironmentAttribute;
import net.minecraft.world.attribute.EnvironmentAttributeMap;
import net.minecraft.world.attribute.EnvironmentAttributeMap.Entry;
import net.minecraft.world.attribute.EnvironmentAttributes;
import net.minecraft.world.attribute.modifier.AttributeModifier;
import net.minecraft.world.level.biome.Biome;
import net.minecraft.world.level.biome.Biome.BiomeBuilder;
import net.minecraft.world.level.biome.BiomeGenerationSettings;
import net.minecraft.world.level.biome.BiomeSpecialEffects;
@@ -42,19 +44,22 @@ public class NMSBiomeInjector {
BiomeSpecialEffects.Builder effects = new BiomeSpecialEffects.Builder();
EnvironmentAttributeMap attributes = vanilla.getAttributes();
Integer vanillaFogColour = (Integer) Objects.requireNonNull(attributes.get(EnvironmentAttributes.FOG_COLOR)).argument();
Integer vanillaWaterFogColour = (Integer) Objects.requireNonNull(attributes.get(EnvironmentAttributes.WATER_FOG_COLOR)).argument();
Integer vanillaSkyColour = (Integer) Objects.requireNonNull(attributes.get(EnvironmentAttributes.SKY_COLOR)).argument();
Float vanillaMusicVolume = (Float) Objects.requireNonNull(attributes.get(EnvironmentAttributes.MUSIC_VOLUME)).argument();
Integer vanillaFogColour = extractInt(attributes, EnvironmentAttributes.FOG_COLOR);
Integer vanillaWaterFogColour = extractInt(attributes, EnvironmentAttributes.WATER_FOG_COLOR);
Integer vanillaSkyColour = extractInt(attributes, EnvironmentAttributes.SKY_COLOR);
Float vanillaMusicVolume = extractFloat(attributes, EnvironmentAttributes.MUSIC_VOLUME);
builder.modifyAttribute(EnvironmentAttributes.FOG_COLOR, AttributeModifier.override(),
Objects.requireNonNullElse(vanillaBiomeProperties.getFogColor(), vanillaFogColour));
builder.modifyAttribute(EnvironmentAttributes.WATER_FOG_COLOR, AttributeModifier.override(),
Objects.requireNonNullElse(vanillaBiomeProperties.getWaterFogColor(), vanillaWaterFogColour));
builder.modifyAttribute(EnvironmentAttributes.SKY_COLOR, AttributeModifier.override(),
Objects.requireNonNullElse(vanillaBiomeProperties.getSkyColor(), vanillaSkyColour));
builder.modifyAttribute(EnvironmentAttributes.MUSIC_VOLUME, AttributeModifier.override(),
Objects.requireNonNullElse(vanillaBiomeProperties.getMusicVolume(), vanillaMusicVolume));
applyIfPresent(builder, EnvironmentAttributes.FOG_COLOR,
vanillaBiomeProperties.getFogColor(), vanillaFogColour);
applyIfPresent(builder, EnvironmentAttributes.WATER_FOG_COLOR,
vanillaBiomeProperties.getWaterFogColor(), vanillaWaterFogColour);
applyIfPresent(builder, EnvironmentAttributes.SKY_COLOR,
vanillaBiomeProperties.getSkyColor(), vanillaSkyColour);
applyIfPresent(builder, EnvironmentAttributes.MUSIC_VOLUME,
vanillaBiomeProperties.getMusicVolume(), vanillaMusicVolume);
effects.waterColor(Objects.requireNonNullElse(vanillaBiomeProperties.getWaterColor(), vanilla.getWaterColor()));
effects.grassColorModifier(
@@ -152,4 +157,31 @@ public class NMSBiomeInjector {
return pack.getID().toLowerCase() + "/" + biomeID.getNamespace().toLowerCase(Locale.ROOT) + "/" + biomeID.getID().toLowerCase(
Locale.ROOT);
}
private static Integer extractInt(EnvironmentAttributeMap attributes,
EnvironmentAttribute<Integer> key) {
Entry<Integer, ?> attr = attributes.get(key);
return attr != null ? (Integer) attr.argument() : null;
}
private static Float extractFloat(EnvironmentAttributeMap attributes,
EnvironmentAttribute<Float> key) {
Entry<Float, ?> attr = attributes.get(key);
return attr != null ? (Float) attr.argument() : null;
}
private static <T> void applyIfPresent(
BiomeBuilder builder,
EnvironmentAttribute<T> attr,
T overrideValue,
T fallbackValue
) {
T value = overrideValue != null ? overrideValue : fallbackValue;
if (value == null) {
return;
}
builder.modifyAttribute(attr, AttributeModifier.override(), value);
}
}