Jump to content

Recommended Posts

Posted (edited)

I am trying to add a stripped version of my custom log to my mod.

Currently, I am stuck because the BLOCK_STRIPPING_MAP has protected access and is final, so I couldn't edit it (to add my custom logs).

Any help on fixing this since there are no online resources to help with this (I tried looking at other mods to see how they did it, but they don't work).

Thanks

 

Here is my VanillaCompat class

public class ModVanillaCompatibility {
    public static void init() {
        registerStrippableBlocks(ModBlocks.BAOBAB_LOG, ModBlocks.STRIPPED_BAOBAB_LOG);
        registerStrippableBlocks(ModBlocks.BAOBAB_WOOD, ModBlocks.STRIPPED_BAOBAB_WOOD);

        ImprovedBiomes.LOGGER.info("VanillaCompat init method registered");
    }

    public static void registerStrippableBlocks(Block log, Block strippedLog) {
        AxeItem.BLOCK_STRIPPING_MAP = Maps.newHashMap(AxeItem.BLOCK_STRIPPING_MAP);
        AxeItem.BLOCK_STRIPPING_MAP.put(log, strippedLog);
    }
}

 

Ask me If I need to upload more code

 

EDIT:

Thanks to all the people who helped me my problem was solved through Reflection (see bottom of the post for the solution).

 

EDIT2:

My solution was bad, since it will not work with other mods, so I am changing it (don't use my solution).

Edited by abdmoh123
Posted (edited)
4 minutes ago, abdmoh123 said:

I am trying to add a stripped version of my custom log to my mod.

Currently, I am stuck because the BLOCK_STRIPPING_MAP has protected access and is final, so I couldn't edit it (to add my custom logs).

Any help on fixing this since there are no online resources to help with this (I tried looking at other mods to see how they did it, but they don't work).

Thanks

 

Here is my VanillaCompat class


public class ModVanillaCompatibility {
    public static void init() {
        registerStrippableBlocks(ModBlocks.BAOBAB_LOG, ModBlocks.STRIPPED_BAOBAB_LOG);
        registerStrippableBlocks(ModBlocks.BAOBAB_WOOD, ModBlocks.STRIPPED_BAOBAB_WOOD);

        ImprovedBiomes.LOGGER.info("VanillaCompat init method registered");
    }

    public static void registerStrippableBlocks(Block log, Block strippedLog) {
        AxeItem.BLOCK_STRIPPING_MAP = Maps.newHashMap(AxeItem.BLOCK_STRIPPING_MAP);
        AxeItem.BLOCK_STRIPPING_MAP.put(log, strippedLog);
    }
}

 

Ask me If I need to upload more code

you might want to do them your self, such as override the onBlockActivated in your custom log, check if the player is holding an axe, and either change the block state or set a new block there. I dont know if it's the best way to do it tho.

Edited by poopoodice
Posted

I looked through other mods, which use a technique similar to this. I really don't want to implement something that already exists in the game. Thanks for the idea though

Posted

Hi

Although I know nothing about the BLOCK_STRIPPING_MAP you can use Reflection to access protected and private fields, and perhaps final members as well (although I've never had to do that myself)

Even if it is final, you can still manipulate the existing map (add/remove elements) even if you can't replace the map itself.

 

And actually - even simpler - Because it's a static field, you can just generate a dummy MyAxeItem extends AxeItem, and get access to the protected field from there, no Reflection required.

 

-TGG

Posted
10 hours ago, TheGreyGhost said:

Even if it is final, you can still manipulate the existing map (add/remove elements) even if you can't replace the map itself.

The map itself is Immutable.

12 hours ago, abdmoh123 said:

I looked through other mods, which use a technique similar to this. I really don't want to implement something that already exists in the game. Thanks for the idea though

The easiest way would be to just use the Block::onBlockActivated method.

 

However if you really don't want to do that with a simple copy-paste and reformat then here's what you have to do.

 

Use Reflection to get access to AxeItem::BLOCK_STRIPPING_MAP store it's contents in another map. Then use Reflection to change it's value to your map. You will use ObfuscationReflectionHelper.getPrivateValue and ObfuscationReflectionHelper.setPrivateValue

VANILLA MINECRAFT CLASSES ARE THE BEST RESOURCES WHEN MODDING

I will be posting 1.15.2 modding tutorials on this channel. If you want to be notified of it do the normal YouTube stuff like subscribing, ect.

Forge and vanilla BlockState generator.

Posted (edited)

Thank you so much for the help.

I used reflection to make the map accessible, then removed the final modifier from the field.

After that, I just made the program refer to another map I made (to add my custom logs to the map).

 

Here is the code I used:

private static Map<Block, Block> TEMP_BLOCK_STRIPPING_MAP = new HashMap<>();

//method below adds vanilla logs to the map
private static void initMap() {
	TEMP_BLOCK_STRIPPING_MAP.put(Blocks.OAK_WOOD, Blocks.STRIPPED_OAK_WOOD);
	TEMP_BLOCK_STRIPPING_MAP.put(Blocks.OAK_LOG, Blocks.STRIPPED_OAK_LOG);
	TEMP_BLOCK_STRIPPING_MAP.put(Blocks.DARK_OAK_WOOD, Blocks.STRIPPED_DARK_OAK_WOOD);
	TEMP_BLOCK_STRIPPING_MAP.put(Blocks.DARK_OAK_LOG, Blocks.STRIPPED_DARK_OAK_LOG);
	TEMP_BLOCK_STRIPPING_MAP.put(Blocks.ACACIA_WOOD, Blocks.STRIPPED_ACACIA_WOOD);
	TEMP_BLOCK_STRIPPING_MAP.put(Blocks.ACACIA_LOG, Blocks.STRIPPED_ACACIA_LOG);
	TEMP_BLOCK_STRIPPING_MAP.put(Blocks.BIRCH_WOOD, Blocks.STRIPPED_BIRCH_WOOD);
	TEMP_BLOCK_STRIPPING_MAP.put(Blocks.BIRCH_LOG, Blocks.STRIPPED_BIRCH_LOG);
	TEMP_BLOCK_STRIPPING_MAP.put(Blocks.JUNGLE_WOOD, Blocks.STRIPPED_JUNGLE_WOOD);
	TEMP_BLOCK_STRIPPING_MAP.put(Blocks.JUNGLE_LOG, Blocks.STRIPPED_JUNGLE_LOG);
	TEMP_BLOCK_STRIPPING_MAP.put(Blocks.SPRUCE_WOOD, Blocks.STRIPPED_SPRUCE_WOOD);
	TEMP_BLOCK_STRIPPING_MAP.put(Blocks.SPRUCE_LOG, Blocks.STRIPPED_SPRUCE_LOG);
}

//this method allows me to add new logs to the map
public static void registerStrippableBlocks(Block log, Block strippedLog) {
	try {
    	TEMP_BLOCK_STRIPPING_MAP.put(log, strippedLog);

        Field field = AxeItem.class.getDeclaredField("BLOCK_STRIPPING_MAP");
        field.setAccessible(true);

        Field modifiersField = Field.class.getDeclaredField("modifiers");
        modifiersField.setAccessible(true);
        modifiersField.setInt(field, field.getModifiers() & ~Modifier.FINAL);

        field.set(null, TEMP_BLOCK_STRIPPING_MAP);
	}
	catch (IllegalAccessException e) {
		ImprovedBiomes.LOGGER.info("Illegal argument");
	}
	catch (NoSuchFieldException e) {
		ImprovedBiomes.LOGGER.info("No such field");
	}
	catch (Exception e) {
		ImprovedBiomes.LOGGER.info("Other exception");
	}
}

 

Edited by abdmoh123
Posted
2 hours ago, abdmoh123 said:

TEMP_BLOCK_STRIPPING_MAP.put(Blocks.OAK_WOOD, Blocks.STRIPPED_OAK_WOOD); TEMP_BLOCK_STRIPPING_MAP.put(Blocks.OAK_LOG, Blocks.STRIPPED_OAK_LOG); TEMP_BLOCK_STRIPPING_MAP.put(Blocks.DARK_OAK_WOOD, Blocks.STRIPPED_DARK_OAK_WOOD); TEMP_BLOCK_STRIPPING_MAP.put(Blocks.DARK_OAK_LOG, Blocks.STRIPPED_DARK_OAK_LOG); TEMP_BLOCK_STRIPPING_MAP.put(Blocks.ACACIA_WOOD, Blocks.STRIPPED_ACACIA_WOOD); TEMP_BLOCK_STRIPPING_MAP.put(Blocks.ACACIA_LOG, Blocks.STRIPPED_ACACIA_LOG); TEMP_BLOCK_STRIPPING_MAP.put(Blocks.BIRCH_WOOD, Blocks.STRIPPED_BIRCH_WOOD); TEMP_BLOCK_STRIPPING_MAP.put(Blocks.BIRCH_LOG, Blocks.STRIPPED_BIRCH_LOG); TEMP_BLOCK_STRIPPING_MAP.put(Blocks.JUNGLE_WOOD, Blocks.STRIPPED_JUNGLE_WOOD); TEMP_BLOCK_STRIPPING_MAP.put(Blocks.JUNGLE_LOG, Blocks.STRIPPED_JUNGLE_LOG); TEMP_BLOCK_STRIPPING_MAP.put(Blocks.SPRUCE_WOOD, Blocks.STRIPPED_SPRUCE_WOOD); TEMP_BLOCK_STRIPPING_MAP.put(Blocks.SPRUCE_LOG, Blocks.STRIPPED_SPRUCE_LOG);

This is not adequate. What if another mod edits the stripping Map the same way you do. You want them to be compatible therefore you need to get the current value using Reflection and take all of the data in it and transfer it into your map. Then replace the map.

 

Also

2 hours ago, abdmoh123 said:

Field field = AxeItem.class.getDeclaredField("BLOCK_STRIPPING_MAP");

This will not work in the actual game IE running it from the Minecraft launcher you must use the obfuscated name of the field which you can get the mappings here.

 

You should also use ObfuscationReflectionHelper instead of raw Reflection.

VANILLA MINECRAFT CLASSES ARE THE BEST RESOURCES WHEN MODDING

I will be posting 1.15.2 modding tutorials on this channel. If you want to be notified of it do the normal YouTube stuff like subscribing, ect.

Forge and vanilla BlockState generator.

Posted

Ok I changed the code for importing the values in the BLOCK_STRIPPING_MAP, but for some reason, my code causes an Illegal Access Exception.

I know the reason is due to the temporary map (TEMP_BLOCK_STRIPPING_MAP), but I don't know how to fix this. I tried mutiple methods (putAll(), for loop etc.) and it still doesn't work.

I even debugged it to inspect the contents of the temporary map (to see if the blocks are added to it); the blocks are successfully put.

 

Any ideas why my code doesn't work?

Thanks for the help

 

private static Map<Block, Block> TEMP_BLOCK_STRIPPING_MAP = new HashMap<>();

private static void initMap() {
	Map<Block, Block> immutableMap = ObfuscationReflectionHelper.getPrivateValue(AxeItem.class, null, "BLOCK_STRIPPING_MAP");

	for (Map.Entry<Block, Block> e : immutableMap.entrySet()) {
		TEMP_BLOCK_STRIPPING_MAP.put(e.getKey().getBlock(), e.getValue().getBlock());
	}
	ImprovedBiomes.LOGGER.info("All items added");
}

public static void registerStrippableBlocks(Block log, Block strippedLog) {
	try {
		TEMP_BLOCK_STRIPPING_MAP.put(log, strippedLog);

		Field field = ObfuscationReflectionHelper.findField(AxeItem.class, "BLOCK_STRIPPING_MAP");

		Field modifiersField = Field.class.getDeclaredField("modifiers");
		modifiersField.setAccessible(true);
		modifiersField.setInt(field, field.getModifiers() & ~Modifier.FINAL);

		field.set(null, TEMP_BLOCK_STRIPPING_MAP);
	}
	catch (IllegalAccessException e) {
		ImprovedBiomes.LOGGER.info("Illegal access");
	}
	catch (NoSuchFieldException e) {
		ImprovedBiomes.LOGGER.info("No such field");
	}
	catch (Exception e) {
		ImprovedBiomes.LOGGER.info("Other exception");
	}
}

 

Posted

I was facing the same problem in my mod. I used the following solution:

private static void addStripping() throws NoSuchFieldException, IllegalAccessException {
    Field map = ObfuscationReflectionHelper.findField(AxeItem.class, "BLOCK_STRIPPING_MAP");

    Field modifiersField = Field.class.getDeclaredField("modifiers");
    modifiersField.setAccessible(true);
    modifiersField.setInt(map, map.getModifiers() & ~Modifier.FINAL);

    map.setAccessible(true);
    Map<Block, Block> strip_map = (Map<Block, Block>) map.get(null);
    HashMap<Block, Block> new_map = new HashMap<>(strip_map);
    new_map.put(Registry.BEECH_LOG_BLOCK, Registry.STRIPPED_BEECH_LOG_BLOCK);
    new_map.put(Registry.BEECH_WOOD_BLOCK, Registry.STRIPPED_BEECH_WOOD_BLOCK);
    map.set(null, new_map);
}

 

Posted
8 minutes ago, abdmoh123 said:

Any ideas why my code doesn't work?

Specifically the

3 minutes ago, deerangle said:

map.setAccessible(true);

 

3 minutes ago, deerangle said:

BLOCK_STRIPPING_MAP

and

3 minutes ago, deerangle said:

modifiers

Need to be the obfuscated names. Check out here.

VANILLA MINECRAFT CLASSES ARE THE BEST RESOURCES WHEN MODDING

I will be posting 1.15.2 modding tutorials on this channel. If you want to be notified of it do the normal YouTube stuff like subscribing, ect.

Forge and vanilla BlockState generator.

Posted

Thanks for the help, my code seems to work now.

All I need to do now is to change the variable names to the obfuscated names.

Sorry for the inconvinence, but I am new to modding with forge, so please can you explain how can I find the names?

 

Thanks

Posted
2 minutes ago, abdmoh123 said:

so please can you explain how can I find the names?

At the link you can download the fields.csv file and open it like a txt file and look for the field names.

VANILLA MINECRAFT CLASSES ARE THE BEST RESOURCES WHEN MODDING

I will be posting 1.15.2 modding tutorials on this channel. If you want to be notified of it do the normal YouTube stuff like subscribing, ect.

Forge and vanilla BlockState generator.

Posted (edited)

Thank you

Also, "modifiers" cannot be obfuscated probably because it's not part of forge (part of java lang); I tested it.

Edited by abdmoh123
Posted

I used the following field name to get this to work (also when building and running outside of the development environment):

ObfuscationReflectionHelper.findField(AxeItem.class, "field_203176_a")

 

Posted

For anyone interested in using the Event method in favor of Reflection:

public class EventHandler {

    public static Map<Block, Block> BLOCK_STRIPPING_MAP = new HashMap<>();

    static {
        BLOCK_STRIPPING_MAP.put(Registry.BEECH_LOG_BLOCK, Registry.STRIPPED_BEECH_LOG_BLOCK);
        BLOCK_STRIPPING_MAP.put(Registry.BEECH_WOOD_BLOCK, Registry.STRIPPED_BEECH_WOOD_BLOCK);
    }

    @SubscribeEvent
    public static void onBlockClicked(PlayerInteractEvent.RightClickBlock event) {
        if (event.getItemStack().getItem() instanceof AxeItem) {
            World world = event.getWorld();
            BlockPos blockpos = event.getPos();
            BlockState blockstate = world.getBlockState(blockpos);
            Block block = BLOCK_STRIPPING_MAP.get(blockstate.getBlock());
            if (block != null) {
                PlayerEntity playerentity = event.getPlayer();
                world.playSound(playerentity, blockpos, SoundEvents.ITEM_AXE_STRIP, SoundCategory.BLOCKS, 1.0F, 1.0F);
                if (!world.isRemote) {
                    world.setBlockState(blockpos, block.getDefaultState()
                            .with(RotatedPillarBlock.AXIS, blockstate.get(RotatedPillarBlock.AXIS)), 11);
                    if (playerentity != null) {
                        event.getItemStack().damageItem(1, playerentity, (p_220040_1_) -> {
                            p_220040_1_.sendBreakAnimation(event.getHand());
                        });
                    }
                }
            }
        }

    }

}

 

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.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Announcements



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.