Jump to content

Replace an block


agowa338

Recommended Posts

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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)

Link to comment
Share on other sites

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.