Changes to sopport proper Block-Tags for Workstations (#39)

This commit is contained in:
Frank 2022-07-26 18:22:57 +02:00
parent 29cf285f09
commit 42fa776320
10 changed files with 75 additions and 6 deletions

View file

@ -7,6 +7,7 @@ import org.betterx.bclib.api.v2.datafixer.DataFixerAPI;
import org.betterx.bclib.api.v2.generator.BCLibEndBiomeSource;
import org.betterx.bclib.api.v2.generator.config.BCLEndBiomeSourceConfig;
import org.betterx.bclib.api.v2.levelgen.biomes.InternalBiomeAPI;
import org.betterx.bclib.api.v2.poi.PoiManager;
import org.betterx.bclib.api.v2.tag.TagAPI;
import org.betterx.bclib.registry.PresetsRegistry;
import org.betterx.worlds.together.tag.v3.TagManager;
@ -43,6 +44,7 @@ public class LevelGenEvents {
WorldEvents.ON_WORLD_LOAD.on(LevelGenEvents::onWorldLoad);
WorldEvents.WORLD_REGISTRY_READY.on(LevelGenEvents::onRegistryReady);
WorldEvents.ON_FINALIZE_LEVEL_STEM.on(LevelGenEvents::finalizeStem);
WorldEvents.ON_FINALIZED_WORLD_LOAD.on(LevelGenEvents::finalizedWorldLoad);
WorldEvents.PATCH_WORLD.on(LevelGenEvents::patchExistingWorld);
WorldEvents.ADAPT_WORLD_PRESET.on(LevelGenEvents::adaptWorldPresetSettings);
@ -50,6 +52,7 @@ public class LevelGenEvents {
WorldEvents.BEFORE_ADDING_TAGS.on(LevelGenEvents::appplyTags);
}
private static void appplyTags(
String directory,
Map<ResourceLocation, List<TagLoader.EntryWithSource>> tagsMap
@ -57,7 +60,6 @@ public class LevelGenEvents {
//make sure we include Tags registered by the deprecated API
TagAPI.apply(directory, tagsMap);
if (directory.equals(TagManager.BIOMES.directory)) {
InternalBiomeAPI._runBiomeTagAdders();
}
@ -156,4 +158,8 @@ public class LevelGenEvents {
) {
InternalBiomeAPI.applyModifications(levelStem.generator().getBiomeSource(), dimension);
}
private static void finalizedWorldLoad(WorldGenSettings worldGenSettings) {
PoiManager.updateStates();
}
}

View file

@ -45,7 +45,7 @@ public class BCLPoiType {
}
public void setTag(TagKey<Block> tag) {
org.betterx.bclib.api.v2.poi.PoiManager.setTag(type, tag);
org.betterx.bclib.api.v2.poi.PoiManager.setTag(key, tag);
}
public Optional<BlockPos> findPoiAround(

View file

@ -1,7 +1,9 @@
package org.betterx.bclib.api.v2.poi;
import org.betterx.bclib.api.v2.levelgen.biomes.InternalBiomeAPI;
import org.betterx.worlds.together.tag.v3.CommonPoiTags;
import net.minecraft.core.Holder;
import net.minecraft.core.Registry;
import net.minecraft.resources.ResourceKey;
import net.minecraft.resources.ResourceLocation;
@ -11,7 +13,7 @@ import net.minecraft.world.entity.ai.village.poi.PoiTypes;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.state.BlockState;
import java.util.Set;
import java.util.*;
import org.jetbrains.annotations.ApiStatus;
public class PoiManager {
@ -27,10 +29,14 @@ public class PoiManager {
}
public static void setTag(ResourceKey<PoiType> type, TagKey<Block> tag) {
setTag(Registry.POINT_OF_INTEREST_TYPE.get(type), tag);
var oHolder = Registry.POINT_OF_INTEREST_TYPE.getHolder(type);
if (oHolder.isPresent()) {
setTag(oHolder.get().value(), tag);
didAddTagFor(oHolder.get(), tag);
}
}
public static void setTag(PoiType type, TagKey<Block> tag) {
private static void setTag(PoiType type, TagKey<Block> tag) {
if ((Object) type instanceof PoiTypeExtension ext) {
ext.bcl_setTag(tag);
}
@ -40,4 +46,40 @@ public class PoiManager {
public static void registerAll() {
PoiManager.setTag(PoiTypes.FISHERMAN, CommonPoiTags.FISHERMAN_WORKSTATION);
}
private static final List<Holder<PoiType>> TYPES_WITH_TAGS = new ArrayList<>(4);
private static Map<BlockState, Holder<PoiType>> ORIGINAL_BLOCK_STATES = null;
private static void didAddTagFor(Holder<PoiType> type, TagKey<Block> tag) {
TYPES_WITH_TAGS.remove(type);
if (tag != null) TYPES_WITH_TAGS.add(type);
}
@ApiStatus.Internal
public static void updateStates() {
if (ORIGINAL_BLOCK_STATES == null) {
//We have not yet tainted the original states, so we will create a copy now
ORIGINAL_BLOCK_STATES = new HashMap<>(PoiTypes.TYPE_BY_STATE);
} else {
//restore unaltered state
PoiTypes.TYPE_BY_STATE.clear();
PoiTypes.TYPE_BY_STATE.putAll(ORIGINAL_BLOCK_STATES);
}
for (Holder<PoiType> type : TYPES_WITH_TAGS) {
if ((Object) type.value() instanceof PoiTypeExtension ex) {
TagKey<Block> tag = ex.bcl_getTag();
if (tag != null) {
var registry = InternalBiomeAPI.worldRegistryAccess().registryOrThrow(tag.registry());
for (var block : registry.getTagOrEmpty(tag)) {
for (var state : block.value().getStateDefinition().getPossibleStates()) {
PoiTypes.TYPE_BY_STATE.put(state, type);
}
}
}
}
}
}
}

View file

@ -5,4 +5,5 @@ import net.minecraft.world.level.block.Block;
public interface PoiTypeExtension {
void bcl_setTag(TagKey<Block> tag);
TagKey<Block> bcl_getTag();
}

View file

@ -16,6 +16,7 @@ import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
public class PoiTypeMixin implements PoiTypeExtension {
private TagKey<Block> bcl_tag = null;
@Inject(method = "is", cancellable = true, at = @At("HEAD"))
void bcl_is(BlockState blockState, CallbackInfoReturnable<Boolean> cir) {
if (bcl_tag != null && blockState.is(bcl_tag)) {
@ -26,4 +27,9 @@ public class PoiTypeMixin implements PoiTypeExtension {
public void bcl_setTag(TagKey<Block> tag) {
bcl_tag = tag;
}
public TagKey<Block> bcl_getTag() {
return bcl_tag;
}
}

View file

@ -0,0 +1,7 @@
package org.betterx.worlds.together.world.event;
import net.minecraft.world.level.levelgen.WorldGenSettings;
public interface OnFinalizeWorldLoad {
void done(WorldGenSettings settings);
}

View file

@ -313,6 +313,8 @@ public class WorldBootstrap {
}
BCLib.LOGGER.info(output);
SurfaceRuleUtil.injectSurfaceRulesToAllDimensions(worldGenSettings);
WorldEventsImpl.ON_FINALIZED_WORLD_LOAD.emit(e -> e.done(worldGenSettings));
}
public static WorldGenSettings enforceInNewWorld(WorldGenSettings worldGenSettings) {

View file

@ -6,6 +6,7 @@ public class WorldEvents {
public static final Event<BeforeServerWorldLoad> BEFORE_SERVER_WORLD_LOAD = WorldEventsImpl.BEFORE_SERVER_WORLD_LOAD;
public static final Event<OnWorldLoad> ON_WORLD_LOAD = WorldEventsImpl.ON_WORLD_LOAD;
public static final Event<OnFinalizeLevelStem> ON_FINALIZE_LEVEL_STEM = WorldEventsImpl.ON_FINALIZE_LEVEL_STEM;
public static final Event<OnFinalizeWorldLoad> ON_FINALIZED_WORLD_LOAD = WorldEventsImpl.ON_FINALIZED_WORLD_LOAD;
public static final Event<OnWorldPatch> PATCH_WORLD = WorldEventsImpl.PATCH_WORLD;
public static final Event<OnAdaptWorldPresetSettings> ADAPT_WORLD_PRESET = WorldEventsImpl.ADAPT_WORLD_PRESET;

View file

@ -10,6 +10,7 @@ public class WorldEventsImpl {
public static final EventImpl<OnWorldLoad> ON_WORLD_LOAD = new EventImpl<>();
public static final EventImpl<OnFinalizeLevelStem> ON_FINALIZE_LEVEL_STEM = new EventImpl<>();
public static final EventImpl<OnFinalizeWorldLoad> ON_FINALIZED_WORLD_LOAD = new EventImpl<>();
public static final PatchWorldEvent PATCH_WORLD = new PatchWorldEvent();
public static final AdaptWorldPresetSettingEvent ADAPT_WORLD_PRESET = new AdaptWorldPresetSettingEvent();

View file

@ -20,3 +20,6 @@ accessible method net/minecraft/world/level/storage/loot/LootPool <init> ([Lnet/
accessible method net/minecraft/world/entity/ai/village/poi/PoiTypes register (Lnet/minecraft/core/Registry;Lnet/minecraft/resources/ResourceKey;Ljava/util/Set;II)Lnet/minecraft/world/entity/ai/village/poi/PoiType;
accessible method net/minecraft/world/level/levelgen/SurfaceRules$SequenceRuleSource <init> (Ljava/util/List;)V
accessible method net/minecraft/core/Registry registerSimple (Lnet/minecraft/resources/ResourceKey;Lnet/minecraft/core/Registry$RegistryBootstrap;)Lnet/minecraft/core/Registry;
#Fields
accessible field net/minecraft/world/entity/ai/village/poi/PoiTypes TYPE_BY_STATE Ljava/util/Map;