Skip to content

Instantly share code, notes, and snippets.

@fmoliveira
Forked from DerFrZocker/CustomDimension.java
Created July 1, 2024 02:06
Show Gist options
  • Save fmoliveira/9d363ff3fe7079f3b6298e01713502a1 to your computer and use it in GitHub Desktop.
Save fmoliveira/9d363ff3fe7079f3b6298e01713502a1 to your computer and use it in GitHub Desktop.
A simple class to change the dimension type of a world. For Spigot and Minecraft version 1.18.1.
/*
* MIT License
*
* Copyright (c) 2022 Marvin (DerFrZocker)
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package de.derfrzocker.testplugin.customdimension;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.dimension.DimensionType;
import org.bukkit.craftbukkit.v1_18_R1.CraftWorld;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.world.WorldInitEvent;
import java.lang.reflect.Field;
import java.util.OptionalLong;
public class CustomDimension implements Listener {
private final static Field FIXED_TIME_FIELD;
private final static Field ININIBURN_FIELD;
private final static Field AMBIENT_LIGHT_FILED;
private final static Field DIMENSION_TYPE_FIELD;
static {
try {
FIXED_TIME_FIELD = DimensionType.class.getDeclaredField("w");
FIXED_TIME_FIELD.setAccessible(true);
ININIBURN_FIELD = DimensionType.class.getDeclaredField("K");
ININIBURN_FIELD.setAccessible(true);
AMBIENT_LIGHT_FILED = DimensionType.class.getDeclaredField("M");
AMBIENT_LIGHT_FILED.setAccessible(true);
DIMENSION_TYPE_FIELD = Level.class.getDeclaredField("C");
DIMENSION_TYPE_FIELD.setAccessible(true);
} catch (NoSuchFieldException e) {
throw new RuntimeException(e);
}
}
@EventHandler
public void onWorldInit(WorldInitEvent event) {
ServerLevel level = ((CraftWorld) event.getWorld()).getHandle();
try {
DIMENSION_TYPE_FIELD.set(level, createCustomDimension(level.dimensionType()));
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
}
}
private DimensionType createCustomDimension(DimensionType original) {
return DimensionType.create(
getFixedTime(original), // fixedTime -> w
original.hasSkyLight(), // hasSkyLight -> x
original.hasCeiling(), // hasCeiling -> y
original.ultraWarm(), // ultraWarm -> z
original.natural(), // natural -> A
original.coordinateScale(), // coordinateScale -> B
original.createDragonFight(), // createDragonFight -> C
original.piglinSafe(), // piglinSafe -> D
original.bedWorks(), // bedWorks -> E
original.respawnAnchorWorks(), // respawnAnchorWorks -> F
original.hasRaids(), // hasRaids -> G
original.minY(), // minY -> H
original.height(), // height -> I
original.logicalHeight(), // logicalHeight -> J
getInfiniburn(original), // infiniburn -> K
original.effectsLocation(), // effectsLocation -> L
getAmbientLight(original) // ambientLight -> M
);
}
private OptionalLong getFixedTime(DimensionType original) {
try {
return (OptionalLong) FIXED_TIME_FIELD.get(original);
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
}
}
private ResourceLocation getInfiniburn(DimensionType original) {
try {
return (ResourceLocation) ININIBURN_FIELD.get(original);
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
}
}
private float getAmbientLight(DimensionType original) {
try {
return (float) AMBIENT_LIGHT_FILED.get(original);
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment