Skip to content

Commit

Permalink
Improve compatibility with PolyMc
Browse files Browse the repository at this point in the history
  • Loading branch information
Patbox committed Sep 9, 2024
1 parent 667c73a commit f844918
Show file tree
Hide file tree
Showing 14 changed files with 92 additions and 31 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ fabric_version=0.102.1+1.21.1

maven_group = eu.pb4

mod_version = 0.9.13
mod_version = 0.9.14

minecraft_version_supported = ">=1.21-"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ public static BlockResourceCreator of(ResourcePackCreator creator) {
}

BlockResourceCreator(ResourcePackCreator creator, BlockExtBlockMapper blockMapper, Runnable onRegister) {
this.states = new HashMap<>(DefaultModelData.USABLE_STATES);
this.models = new HashMap<>(DefaultModelData.MODELS);
this.states = new EnumMap<>(DefaultModelData.USABLE_STATES);
this.models = new IdentityHashMap<>(DefaultModelData.MODELS);
this.creator = creator;
this.blockMapper = blockMapper;
this.onRegister = onRegister;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@
import net.minecraft.server.network.ServerPlayerEntity;

import java.util.HashMap;
import java.util.IdentityHashMap;
import java.util.Map;

public class BlockExtBlockMapper implements BlockMapper {
public static final BlockExtBlockMapper INSTANCE = new BlockExtBlockMapper(BlockMapper.createDefault());

public final Map<BlockState, BlockState> stateMap = new HashMap<>();
public final Map<BlockState, BlockState> stateMap = new IdentityHashMap<>();
private final BlockMapper baseMapper;

public BlockExtBlockMapper(BlockMapper baseMapper) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,13 @@
import net.minecraft.state.property.Properties;
import net.minecraft.util.Identifier;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.*;
import java.util.function.Predicate;

public class DefaultModelData {
public static final Map<BlockModelType, List<BlockState>> USABLE_STATES = new HashMap<>();
public static final Map<BlockState, BlockState> SPECIAL_REMAPS = new HashMap<>();
public static final Map<BlockState, PolymerBlockModel[]> MODELS = new HashMap<>();
public static final Map<BlockModelType, List<BlockState>> USABLE_STATES = new EnumMap<>(BlockModelType.class);
public static final Map<BlockState, BlockState> SPECIAL_REMAPS = new IdentityHashMap<>();
public static final Map<BlockState, PolymerBlockModel[]> MODELS = new IdentityHashMap<>();

private static final Predicate<BlockState> WATERLOGGED_PREDICATE = (state -> state.getBlock() instanceof Waterloggable && state.get(Properties.WATERLOGGED));
private static final Predicate<BlockState> NOT_WATERLOGGED_PREDICATE = (state -> !(state.getBlock() instanceof Waterloggable && state.get(Properties.WATERLOGGED)));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package eu.pb4.polymer.blocks.impl;

import eu.pb4.polymer.core.api.block.PolymerBlockUtils;
import io.github.theepicblock.polymc.api.block.BlockPoly;
import net.minecraft.block.BlockState;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package eu.pb4.polymer.blocks.mixin.polymc;

import eu.pb4.polymer.blocks.impl.BlockExtBlockMapper;
import io.github.theepicblock.polymc.api.PolyMap;
import net.minecraft.block.BlockState;
import net.minecraft.server.network.ServerPlayerEntity;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Pseudo;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;

@Pseudo
@Mixin(PolyMap.class)
public interface PolyMapImplMixin {
@Inject(method = "getClientState", at = @At("HEAD"), cancellable = true, require = 0)
private void skipPolymerHandledBlocks(BlockState serverBlock, ServerPlayerEntity player, CallbackInfoReturnable<BlockState> cir) {
if (BlockExtBlockMapper.INSTANCE.stateMap.containsKey(serverBlock)) {
cir.setReturnValue(serverBlock);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ private void polymer_block_catchRegistry(CallbackInfo ci) {
try {
// Request this specific state from PolyMc
// This will cause PolyMc to mark it as used
blockStateManager.requestBlockState(state -> state == takenState, new Block[]{takenState.getBlock()}, (block, registry) -> { });
blockStateManager.requestBlockState(state -> state == takenState, new Block[]{takenState.getBlock()}, (block, registry) -> {

});
} catch (Exception e) {
e.printStackTrace();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"compatibilityLevel": "JAVA_21",
"plugin": "eu.pb4.polymer.blocks.mixin.PolymerBlocksMixinConfigPlugin",
"mixins": [
"polymc.PolyMapImplMixin",
"polymc.PolyRegistryMixin"
],
"client": [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public Identifier getId(T value) {

@Override
public Optional<RegistryKey<T>> getKey(T entry) {
return Optional.empty();
return Optional.of(RegistryKey.of(registryKey, defaultId));
}

@Override
Expand Down Expand Up @@ -66,7 +66,7 @@ public T get(@Nullable Identifier id) {

@Override
public Optional<RegistryEntryInfo> getEntryInfo(RegistryKey<T> key) {
return Optional.empty();
return Optional.of(RegistryEntryInfo.DEFAULT);
}

@Override
Expand All @@ -76,7 +76,7 @@ public Lifecycle getLifecycle() {

@Override
public Optional<RegistryEntry.Reference<T>> getDefaultEntry() {
return Optional.empty();
return Optional.of(createEntry(defaultValue));
}

@Override
Expand Down Expand Up @@ -121,12 +121,12 @@ public RegistryEntry.Reference<T> createEntry(T value) {

@Override
public Optional<RegistryEntry.Reference<T>> getEntry(int rawId) {
return Optional.empty();
return getDefaultEntry();
}

@Override
public Optional<RegistryEntry.Reference<T>> getEntry(Identifier id) {
return Optional.empty();
return getDefaultEntry();
}

@Override
Expand All @@ -141,7 +141,7 @@ public RegistryEntry<T> getEntry(T value) {

@Override
public Stream<RegistryEntry.Reference<T>> streamEntries() {
return null;
return Stream.empty();
}

@Override
Expand Down Expand Up @@ -204,7 +204,7 @@ public Stream<RegistryEntryList.Named<T>> streamTags() {

@Override
public Optional<RegistryEntry.Reference<T>> getOptional(RegistryKey<T> key) {
return Optional.empty();
return getDefaultEntry();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,16 @@
import net.minecraft.entity.damage.DamageScaling;
import net.minecraft.entity.damage.DamageSources;
import net.minecraft.entity.damage.DamageType;
import net.minecraft.entity.decoration.painting.PaintingVariant;
import net.minecraft.entity.passive.WolfVariant;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.fluid.Fluid;
import net.minecraft.item.map.MapState;
import net.minecraft.recipe.BrewingRecipeRegistry;
import net.minecraft.recipe.RecipeManager;
import net.minecraft.registry.*;
import net.minecraft.registry.entry.RegistryEntry;
import net.minecraft.registry.entry.RegistryEntryList;
import net.minecraft.registry.entry.RegistryEntryOwner;
import net.minecraft.registry.tag.BlockTags;
import net.minecraft.resource.featuretoggle.FeatureFlags;
Expand All @@ -29,6 +32,7 @@
import net.minecraft.sound.SoundEvent;
import net.minecraft.util.Identifier;
import net.minecraft.util.TypeFilter;
import net.minecraft.util.Util;
import net.minecraft.util.function.LazyIterationConsumer;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Box;
Expand Down Expand Up @@ -76,22 +80,18 @@ public final class FakeWorld extends World implements LightSourceView {
static final Scoreboard SCOREBOARD = new Scoreboard();

static final DynamicRegistryManager FALLBACK_REGISTRY_MANAGER = new DynamicRegistryManager.Immutable() {
private final FakeRegistry<DamageType> damageTypes = new FakeRegistry<>(RegistryKeys.DAMAGE_TYPE, Identifier.of("polymer","fake_damage"),
new DamageType("", DamageScaling.NEVER, 0));
private final FakeRegistry<BannerPattern> bannerPatterns = new FakeRegistry<>(RegistryKeys.BANNER_PATTERN,
Identifier.of("polymer","fake_pattern"),
new BannerPattern(Identifier.of("polymer","fake_pattern"), ""));
private static final Map<RegistryKey<?>, Registry<?>> REGISTRIES = new HashMap<>();
@Override
public Optional<Registry> getOptional(RegistryKey key) {
var x = Registries.REGISTRIES.get(key);
if (x != null) {
return Optional.of(x);
}

if (RegistryKeys.DAMAGE_TYPE.equals(key)) {
return Optional.of(damageTypes);
} else if (RegistryKeys.BANNER_PATTERN.equals(key)) {
return Optional.of(bannerPatterns);
var reg = REGISTRIES.get(key);

if (reg != null) {
return Optional.of(reg);
}

return Optional.empty();
Expand All @@ -101,6 +101,24 @@ public Optional<Registry> getOptional(RegistryKey key) {
public Stream<Entry<?>> streamAllRegistries() {
return Stream.empty();
}

public static void addRegistry(FakeRegistry<?> registry) {
REGISTRIES.put(registry.getKey(), registry);
}

static {
addRegistry(new FakeRegistry<>(RegistryKeys.DAMAGE_TYPE, Identifier.of("polymer","fake_damage"),
new DamageType("", DamageScaling.NEVER, 0)));
addRegistry(new FakeRegistry<>(RegistryKeys.BANNER_PATTERN,
Identifier.of("polymer","fake_pattern"),
new BannerPattern(Identifier.of("polymer","fake_pattern"), "")));
addRegistry(new FakeRegistry<>(RegistryKeys.PAINTING_VARIANT,
Identifier.of("polymer","painting"),
new PaintingVariant(1, 1, Identifier.of("polymer","painting"))));
addRegistry(new FakeRegistry<>(RegistryKeys.WOLF_VARIANT,
Identifier.of("polymer","wolf"),
new WolfVariant(Identifier.of("polymer","wolf"), Identifier.of("polymer","wolf"),Identifier.of("polymer","wolf"), RegistryEntryList.empty())));
}
};
static final RecipeManager RECIPE_MANAGER = new RecipeManager(FALLBACK_REGISTRY_MANAGER);
private static final FeatureSet FEATURES = FeatureFlags.FEATURE_MANAGER.getFeatureSet();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,8 @@ public final class PolymerItemUtils {
DataComponentTypes.POTION_CONTENTS,
DataComponentTypes.CUSTOM_NAME,
DataComponentTypes.JUKEBOX_PLAYABLE,
DataComponentTypes.WRITABLE_BOOK_CONTENT,
DataComponentTypes.WRITTEN_BOOK_CONTENT,
DataComponentTypes.CONTAINER,
};
@SuppressWarnings("rawtypes")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,15 @@ public class PolymerResourcePackImpl {
public static final List<String> INCLUDE_ZIPS;
public static final UUID MAIN_UUID;
public static final Path DEFAULT_PATH;
public static final String FILE_NAME;
public static final List<String> PREVENTED_PATHS;


static {
var config = CommonImpl.loadConfig("resource-pack", Config.class);

DEFAULT_PATH = FabricLoader.getInstance().getGameDir().resolve(config.resourcePackPath);
FILE_NAME = config.resourcePackPath;

MAIN_UUID = config.mainUuid;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ public static void generateAndCall(MinecraftServer server, boolean ignoreLock, C
alreadyGeneration = false;
if (success) {
messageConsumer.accept(Text.literal("[Polymer] Resource pack created successfully! You can find it in game folder as ")
.append(Text.literal("polymer-resourcepack.zip")
.append(Text.literal(PolymerResourcePackImpl.FILE_NAME)
.setStyle(Style.EMPTY.withUnderline(true)
.withHoverEvent(new HoverEvent(
HoverEvent.Action.SHOW_TEXT,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import eu.pb4.polymer.common.impl.CommonImpl;
import eu.pb4.polymer.resourcepack.api.ResourcePackBuilder;
import eu.pb4.polymer.resourcepack.impl.PolymerResourcePackMod;
import io.github.theepicblock.polymc.PolyMc;
import io.github.theepicblock.polymc.impl.misc.logging.SimpleLogger;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.command.ServerCommandSource;
import org.jetbrains.annotations.ApiStatus;
Expand All @@ -19,7 +21,22 @@
public class PolyMcHelpers {
public static void importPolyMcResources(ResourcePackBuilder builder) {
// Generate PolyMc's resource pack
var pack = io.github.theepicblock.polymc.PolyMc.getMapForResourceGen().generateResourcePack(io.github.theepicblock.polymc.PolyMc.LOGGER);
var pack = io.github.theepicblock.polymc.PolyMc.getMapForResourceGen().generateResourcePack(new SimpleLogger() {
@Override
public void error(String string) {
PolyMc.LOGGER.error(string);
}

@Override
public void warn(String string) {
PolyMc.LOGGER.warn(string);
}

@Override
public void info(String string) {
// Don't care
}
});
if (pack == null) return;

// Directly write each of PolyMc's assets to a byte array in memory. This prevents the need to write it to disk
Expand Down

0 comments on commit f844918

Please sign in to comment.