Jump to content
View in the app

A better way to browse. Learn more.

Forge Forums

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Featured Replies

Posted

I want to change the behavior of the farmland block. So I've made a simple block, that extends it.

Bevore 1.7 I've used

Block.blocksList[FarmlandBlockID] = null;
Block newfarmland = (new BlockFarmlandNew(FarmlandBlockID)).setUnlocalizedName("farmland").setTextureName("farmland");
GameRegistry.registerBlock(newfarmland, "farmland");

but blocksList was removed, does anybody know another way to remove an registred block?

Item and Block mappings are stored in the GameData class.

You can get a block mapping by using the GameData.blockRegistry.get("NAME"); command but there is no real method to easily remove that entry. You could try using the itreator of the map but I have not tried that.

PM's regarding modding questions should belong in the Modder Support sub-forum and won't be answered.

  • Author

I now found another way to change the Block.

What I did for that was to create a new event called UseHoe and recoded the part where the dirt block is swapped with the farmland but I had to do that without par7 (so I did "int par7 =1") from onItemUse (net.minecraft.item.ItemHoe).

Inside that event I swapped the Line

Block block1 = Block.farmland;

with

Block block1 = (Block)Block.blockRegistry.getObject("BetterFarmLand:BlockFarmLandNew");

 

Does anybody know what par7 is used for?

 

 

__________________________

What I have also tryed was a reflektion of

public static final Block farmland = (Block)Block.blockRegistry.getObject("farmland");

to

public static final Block farmland = (Block)Block.blockRegistry.getObject("BetterFarmLand:BlockFarmLandNew");

 

Then I was seeing, that the blocks were swapped but there was no difference in the game, but (System.out.println showed me that the Block farmland was BlockFarmLandNew)

Does anybody have any idea?

Create class:

public class BlockReplaceHelper{
public static boolean replaceBlock(Block toReplace, Class<? extends Block> blockClass){
	Field modifiersField=null;
    	try{
    		modifiersField=Field.class.getDeclaredField("modifiers");
    		modifiersField.setAccessible(true);
    		
    		for(Field field:Blocks.class.getDeclaredFields()){
        		if (Block.class.isAssignableFrom(field.getType())){
    				Block block=(Block)field.get(null);
    				if (block==toReplace){
    					String registryName=Block.blockRegistry.getNameForObject(block);
    					int id=Block.getIdFromBlock(block);
    					ItemBlock item=(ItemBlock)Item.getItemFromBlock(block);
    					System.out.println("Replacing block - "+id+"/"+registryName);
    					
    					Block newBlock=blockClass.newInstance();
    					FMLControlledNamespacedRegistry<Block> registry=GameData.blockRegistry;
    					registry.putObject(registryName,newBlock);
    					
    					Field map=RegistryNamespaced.class.getDeclaredFields()[0];
    					map.setAccessible(true);
    					((ObjectIntIdentityMap)map.get(registry)).func_148746_a(newBlock,id);
    					
    					map=FMLControlledNamespacedRegistry.class.getDeclaredField("namedIds");
    					map.setAccessible(true);
    					((BiMap)map.get(registry)).put(registryName,id);
    					
    					field.setAccessible(true);
    					int modifiers=modifiersField.getInt(field);
    					modifiers&=~Modifier.FINAL;
    					modifiersField.setInt(field,modifiers);
    					field.set(null,newBlock);
    					
    					Field itemblock=ItemBlock.class.getDeclaredFields()[0];
    					itemblock.setAccessible(true);
    					modifiers=modifiersField.getInt(itemblock);
    					modifiers&=~Modifier.FINAL;
    					modifiersField.setInt(itemblock,modifiers);
    					itemblock.set(item,newBlock);
    					
    					System.out.println("Check field: "+field.get(null).getClass());
    					System.out.println("Check registry: "+Block.blockRegistry.getObjectById(id).getClass());
    					System.out.println("Check item: "+((ItemBlock)Item.getItemFromBlock(newBlock)).field_150939_a.getClass());
    				}
        		}
        	}
    	}catch(Exception e){
    		e.printStackTrace();
    		return false;
    	}
    	return true;
}
}

And BlockReplaceHelper.replaceBlock(Block.farmLand, NewFarmLand.class)

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...

Important Information

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

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.