Skip to content

Commit

Permalink
remove dim data fix jar in jar. fix dynamic dimension not removed fro…
Browse files Browse the repository at this point in the history
…m registry
  • Loading branch information
qouteall committed Aug 19, 2023
1 parent 5e6a104 commit e853c7c
Show file tree
Hide file tree
Showing 10 changed files with 142 additions and 37 deletions.
4 changes: 0 additions & 4 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,6 @@ allprojects {

implementation("com.github.llamalad7.mixinextras:mixinextras-fabric:${mixin_extras_version}")
annotationProcessor("com.github.llamalad7.mixinextras:mixinextras-fabric:${mixin_extras_version}")

modImplementation "com.github.qouteall:DimensionDataFix:${dim_data_fix_version}"
}

processResources {
Expand Down Expand Up @@ -180,8 +178,6 @@ dependencies {

include("com.github.llamalad7.mixinextras:mixinextras-fabric:${mixin_extras_version}")

include "com.github.qouteall:DimensionDataFix:${dim_data_fix_version}"

modLocalRuntime "maven.modrinth:modmenu:${modmenu_version}"

if (project.enable_sodium.equals('true')) {
Expand Down
3 changes: 1 addition & 2 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ archives_base_name=immersive-portals
modmenu_version=7.0.1
cloth_config_version=11.0.99
mixin_extras_version=0.2.0-beta.9
dim_data_fix_version=64f92faa
sodium_path=maven.modrinth:sodium:mc1.20.1-0.5.2
iris_path=maven.modrinth:iris:1.6.8+1.20.1
gravity_changer_version=v1.0.2-mc1.20.1
Expand All @@ -28,7 +27,7 @@ yarn_mappings=1.20.1+build.10
loader_version=0.14.21

#Fabric api
fabric_version=0.86.0+1.20.1
fabric_version=0.87.0+1.20.1


# for testing with create
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

import static me.jellysquid.mods.sodium.client.render.chunk.lists.SortedRenderLists.Builder;

@Mixin(RenderRegion.class)
@Mixin(value = RenderRegion.class, remap = false)
public class MixinSodiumRenderRegion {
@Shadow
@Final
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import org.spongepowered.asm.mixin.injection.Redirect;
import qouteall.imm_ptl.core.compat.sodium_compatibility.SodiumInterface;

@Mixin(Viewport.class)
@Mixin(value = Viewport.class, remap = false)
public class MixinSodiumViewport {
@Redirect(
method = "isBoxVisible",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,15 @@ public static void register(CommandDispatcher<CommandSourceStack> dispatcher) {
templateDimension, newDimId
);

// context.getSource().sendSuccess(
// Component.literal("Warning: the dynamic dimension feature is not yet stable now"),
// false
// );

context.getSource().sendSuccess(() -> Component.literal(
"Dynamically added dimension %s".formatted(newDimensionId)
), true);

context.getSource().sendSuccess(
() -> Component.literal("Warning: In the current version, dynamic dimension feature is still experimental and not yet stable."),
false
);

return 0;
})
)
Expand All @@ -64,16 +65,16 @@ public static void register(CommandDispatcher<CommandSourceStack> dispatcher) {

DimensionAPI.deleteDimensionConfiguration(dimension.dimension());

// context.getSource().sendSuccess(
// Component.literal("Warning: the dynamic dimension feature is not yet stable now"),
// false
// );

context.getSource().sendSuccess(() -> Component.literal(
"Dynamically removed dimension %s . Its world file is not yet deleted."
.formatted(dimension.dimension().location())
), true);

context.getSource().sendSuccess(
() -> Component.literal("Warning: In the current version, dynamic dimension feature is still experimental and not yet stable."),
false
);

return 0;
})

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
package qouteall.q_misc_util.dimension;

import com.google.common.collect.ImmutableList;
import com.mojang.serialization.DynamicOps;
import com.mojang.serialization.Lifecycle;
import net.minecraft.core.BlockPos;
import net.minecraft.core.MappedRegistry;
import net.minecraft.core.Registry;
import net.minecraft.core.RegistryAccess;
import net.minecraft.core.registries.Registries;
import net.minecraft.network.chat.Component;
import net.minecraft.network.protocol.Packet;
import net.minecraft.resources.ResourceKey;
Expand All @@ -17,6 +23,8 @@
import net.minecraft.world.level.border.WorldBorder;
import net.minecraft.world.level.chunk.ChunkStatus;
import net.minecraft.world.level.dimension.LevelStem;
import net.minecraft.world.level.levelgen.WorldGenSettings;
import net.minecraft.world.level.levelgen.WorldOptions;
import net.minecraft.world.level.storage.DerivedLevelData;
import net.minecraft.world.level.storage.ServerLevelData;
import net.minecraft.world.level.storage.WorldData;
Expand All @@ -27,7 +35,9 @@
import qouteall.q_misc_util.MiscHelper;
import qouteall.q_misc_util.MiscNetworking;
import qouteall.q_misc_util.api.DimensionAPI;
import qouteall.q_misc_util.ducks.IEMappedRegistry2;
import qouteall.q_misc_util.ducks.IEMinecraftServer_Misc;
import qouteall.q_misc_util.mixin.dimension.IEMappedRegistry;
import qouteall.q_misc_util.mixin.dimension.IEWorldBorder;
import qouteall.q_misc_util.my_util.MyTaskList;
import qouteall.q_misc_util.my_util.SignalArged;
Expand Down Expand Up @@ -94,6 +104,19 @@ public static void addDimensionDynamically(

((IEMinecraftServer_Misc) server).ip_addDimensionToWorldMap(dimensionResourceKey, newWorld);

/**
* register it into registry, so it will be saved in
* {@link WorldGenSettings#encode(DynamicOps, WorldOptions, RegistryAccess)} ,
* so it will be saved into level.dat
* */
Registry<LevelStem> levelStemRegistry = server.registryAccess().registryOrThrow(Registries.LEVEL_STEM);
((IEMappedRegistry) levelStemRegistry).ip_setIsFrozen(false);
((MappedRegistry<LevelStem>) levelStemRegistry).register(
ResourceKey.create(Registries.LEVEL_STEM, dimensionId),
levelStem, Lifecycle.stable()
);
((IEMappedRegistry) levelStemRegistry).ip_setIsFrozen(true);

worldBorder.applySettings(serverLevelData.getWorldBorder());

Helper.log("Added Dimension " + dimensionId);
Expand Down Expand Up @@ -181,6 +204,11 @@ public static void removeDimensionDynamically(ServerLevel world) {

resetWorldBorderListener(server);

// force remove it from registry, so it will not be saved into level.dat
Registry<LevelStem> levelStemRegistry = server.registryAccess()
.registryOrThrow(Registries.LEVEL_STEM);
((IEMappedRegistry2) levelStemRegistry).ip_forceRemove(dimension.location());

Helper.log("Successfully Removed Dimension " + dimension.location());

Packet dimSyncPacket = MiscNetworking.createDimSyncPacket();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package qouteall.q_misc_util.ducks;

import net.minecraft.resources.ResourceLocation;

// TODO merge it with IEMappedRegistry
public interface IEMappedRegistry2 {
public boolean ip_forceRemove(ResourceLocation id);
}
Original file line number Diff line number Diff line change
@@ -1,25 +1,95 @@
package qouteall.q_misc_util.mixin;

import com.mojang.serialization.Lifecycle;
import it.unimi.dsi.fastutil.objects.Object2IntMap;
import it.unimi.dsi.fastutil.objects.ObjectList;
import net.minecraft.core.Holder;
import net.minecraft.core.MappedRegistry;
import net.minecraft.core.Registry;
import net.minecraft.resources.ResourceKey;
import net.minecraft.resources.ResourceLocation;
import org.jetbrains.annotations.Nullable;
import org.slf4j.Logger;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import qouteall.q_misc_util.ducks.IEMappedRegistry2;

import java.util.List;
import java.util.Map;

@Mixin(MappedRegistry.class)
public class MixinMappedRegistry<T> {
// @ModifyVariable(
// method = "registerMapping(ILnet/minecraft/resources/ResourceKey;Ljava/lang/Object;Lcom/mojang/serialization/Lifecycle;Z)Lnet/minecraft/core/Holder;",
// at = @At("HEAD"),
// argsOnly = true,
// index = 4
// )
// private Lifecycle modifyLifecycle(
// Lifecycle value,
// int i, ResourceKey<T> resourceKey, T object, Lifecycle lifecycle, boolean bl
// ) {
// if (MiscGlobals.stableNamespaces.contains(resourceKey.location().getNamespace())) {
// return Lifecycle.stable();
// }
// else {
// return lifecycle;
// }
// }
public abstract class MixinMappedRegistry<T> implements IEMappedRegistry2 {
@Shadow
@Final
private Map<ResourceLocation, Holder.Reference<T>> byLocation;

@Shadow
@Final
private static Logger LOGGER;

@Shadow
public abstract @Nullable T byId(int id);

@Shadow
@Final
private ObjectList<Holder.Reference<T>> byId;

@Shadow
@Final
private Object2IntMap<T> toId;

@Shadow
@Final
private Map<ResourceKey<T>, Holder.Reference<T>> byKey;

@Shadow
@Final
private ResourceKey<? extends Registry<T>> key;

@Shadow
@Final
private Map<T, Holder.Reference<T>> byValue;

@Shadow
@Final
private Map<T, Lifecycle> lifecycles;

@Shadow
private @Nullable List<Holder.Reference<T>> holdersInOrder;

@Override
public boolean ip_forceRemove(ResourceLocation id) {
Holder.Reference<T> holder = byLocation.remove(id);

if (holder == null) {
return false;
}

T value = holder.value();

if (value == null) {
LOGGER.error("[ImmPtl] missing value in holder {}", holder);
return false;
}

int intId = toId.getInt(value);

if (intId == -1) {
LOGGER.error("[ImmPtl] missing integer id for {}", value);
}
else {
toId.removeInt(value);
byId.set(intId, null);
}

byKey.remove(ResourceKey.create(key, id));
byValue.remove(value);
lifecycles.remove(value);

holdersInOrder = null;

return true;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ public interface IEMappedRegistry {
@Accessor("frozen")
boolean ip_getIsFrozen();

/**
* Note: un-freeze is only safe when no place use its holder.
*/
@Accessor("frozen")
void ip_setIsFrozen(boolean cond);
}
2 changes: 1 addition & 1 deletion src/main/resources/fabric.mod.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
],
"depends": {
"fabricloader": ">=0.7.4",
"fabric-api": ">=0.28.1",
"fabric-api": ">=0.87.0",
"minecraft": ["1.20.1", "1.20"],
"imm_ptl_core": "*",
"q_misc_util": "*"
Expand Down

0 comments on commit e853c7c

Please sign in to comment.