Posted May 18, 20205 yr (I apologize for bad english) Hello, I have tried to overwrite the vanilla GenerationSettings using Java.Reflection. Settings override class: (This code will run, if the LoadCompleteEvent fire) public static void overrideSettings() { try { Class<?> GenClass = Class.forName("net.minecraft.world.gen.GenerationSettings"); Field defaultBlockField = GenClass.getDeclaredField("defaultBlock"); GenerationSettings gen = (GenerationSettings) GenClass.newInstance(); defaultBlockField.setAccessible(true); Field modifiers = Field.class.getDeclaredField("modifiers"); modifiers.setAccessible(true); modifiers.setInt(defaultBlockField, defaultBlockField.getModifiers() & ~Modifier.FINAL); defaultBlockField.set(gen, Blocks.DIAMOND_BLOCK.getDefaultState()); StickOfDebug.s = new String(defaultBlockField.get(gen).toString()); } catch(Exception e) { StickOfDebug.s = new String(e.toString()); } } The Field with the "defaultBlock" have been successfully overwritten and the field give me the diamond_block and I don't get any exception. But when I generate a new world, the defaultBlock is still stone_block. If I using the my Debug_Stick, the GenerationSettings still give me the stone_block value back. The debug stick class: public class StickOfDebug extends Item { public static String s = ""; public StickOfDebug(Properties properties) { super(properties); } @Override public ActionResult<ItemStack> onItemRightClick(World worldIn, PlayerEntity playerIn, Hand handIn) { ItemStack itemstack = playerIn.getHeldItem(handIn); if(worldIn.isRemote) { return ActionResult.resultPass(itemstack); } else { playerIn.sendMessage(new StringTextComponent(s)); try { Class<?> genClass = Class.forName("net.minecraft.world.gen.GenerationSettings"); GenerationSettings gen = (GenerationSettings) genClass.newInstance(); Field field = genClass.getDeclaredField("defaultBlock"); field.setAccessible(true); playerIn.sendMessage(new StringTextComponent(field.get(gen).toString())); } catch (Exception e) { e.printStackTrace(); } return ActionResult.resultSuccess(itemstack); } } } I would appreciate if someone could help me. Edited May 18, 20205 yr by DarkLions This question is solved for me.
May 18, 20205 yr Well, your code (if it worked at all) will only work in the development environment. Because that string is not the actual name of the compiled vanilla class (any reason you aren't using Class GenClass = GenerationSettings.class?) or the field name when compiled. There's a reason Forge supplies an ObfuscationReflectionHelper class. As for why its not changing, I don't know. When and where are you calling that code? Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable. If you think this is the case, JUST REPORT ME. Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice. Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked. DO NOT PM ME WITH PROBLEMS. No help will be given.
May 18, 20205 yr Author Thanks for your reply. The code is seperated from the main class and it will called if the "FMLLoadCompleteEvent" is fired. I have tried (Class GenClass = GenerationSettings.class) seem also not working. So have tried to implement the ObfuscationReflectionHelper, but I get the same issue. It isn't changing anything. I'm not sure, if it is because I call a newInstance for the object. the code in his own class: (only changed the class.getDeclaredField to ObfuscationReflection) package com.darklions.testmod.world.gen; import java.lang.reflect.Field; import java.lang.reflect.Modifier; import com.darklions.testmod.objects.items.StickOfDebug; import net.minecraft.block.Blocks; import net.minecraft.world.gen.GenerationSettings; import net.minecraftforge.fml.common.ObfuscationReflectionHelper; public class VanillaGenSettingsOverride { public static void overrideSettings() { try { Class<?> GenClass = GenerationSettings.class; //used this method instead of GenClass.getDeclaredField Field defaultBlockField = ObfuscationReflectionHelper.findField(GenClass, "defaultBlock"); GenerationSettings gen = (GenerationSettings) GenClass.newInstance(); Field modifiers = Field.class.getDeclaredField("modifiers"); modifiers.setAccessible(true); modifiers.setInt(defaultBlockField, defaultBlockField.getModifiers() & ~Modifier.FINAL); defaultBlockField.set(gen, Blocks.DIAMOND_BLOCK.getDefaultState()); //this give me the diamond_block value StickOfDebug.s = new String(defaultBlockField.get(gen).toString()); } catch(Exception e) { StickOfDebug.s = new String(e.toString()); } } } @SubscribeEvent public static void loadCompleteEvent(FMLLoadCompleteEvent event) { CustomOreGen.setupOverWorldOreGeneration(); CustomOreGen.setupEndOreGeneration(); CustomOreGen.setupNetherOreGeneration(); CustomWorldCarverGen.setupEndCarverGeneration(); VanillaGenSettingsOverride.overrideSettings(); } And this is in the main class.
May 18, 20205 yr My guess would be that when you go into generate a new world, the values are being set by something else, overwriting your change. Go look at the field, what sets its value, and what calls the code that sets its value. Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable. If you think this is the case, JUST REPORT ME. Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice. Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked. DO NOT PM ME WITH PROBLEMS. No help will be given.
May 18, 20205 yr Author So I have found out what the problem was. The Overworlddimension has overwrite the defaultBlock value. Because the value is in the constructor defined, I can‘t change it directly. or is impossible to change. The best is I just create a custom Dimension and settings. Thank you for your support :D
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.