Jump to content

[1.11.2] Removing blocks when picked from the world


aetherean

Recommended Posts

Hello,

 

I'm very new to Minecraft Forge modding and Minecraft in general, so please excuse my ignorance.

I'm trying to define the behavior of my custom block such that when it is picked in Creative, it will disappear from the world (but the item will be placed in your hand as it would with normal picking of blocks). I would also like to increment its inventory count when it is picked (and similarly decrement it when it is placed).

I've been trying to go about this by overriding the getPickBlock method definition and using within it world.setBlockToAir(pos), but from what I understand, getPickBlock is never called server-side, so I run into the problem of still colliding with air when blocks are picked. I don't quite understand the server-client relationship -- where should I be trying to do this instead? Or, is there a better solution?

 

My (wrong) code:

@Override
public ItemStack getPickBlock(IBlockState state, RayTraceResult target, World world, BlockPos pos, EntityPlayer player) {
   player.inventory.setPickedItemStack(getItem(world, pos, state));
   player.inventory.getCurrentItem().grow(1);
   world.setBlockToAir(pos);
   return getItem(world, pos, state);
}

 

Thanks in advance.

Link to comment
Share on other sites

WHY would you ever want to do this?

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.

Link to comment
Share on other sites

I'm trying to simulate an environment in which users can pick up/set down blocks from a limited quantity (instead of "destroying" blocks that is traditionally done), while still being able to make structures in Creative mode while flying around. I'm trying to use Minecraft as a base where users feel like they're "playing with toy blocks" rather than "playing Minecraft," if that makes sense. 

 

Does it make sense to approach it from this way? Or should I instead do something like overriding the "destroy" block functions instead of "pick block"?

Link to comment
Share on other sites

I see, thanks for the input. Then in order to get the behavior I need in Creative mode (destroying a block adds to your item stack in the inventory of that item, while placing blocks consumes one of the items in that stack), what would be easiest? Should I look into modifying the behavior of the block's destroy function? Harvest function + auto-pickup for drops? Or is limited resources a thing I can enable in Creative and I'm just not savvy enough with Minecraft to know about it?

Edited by aetherean
Link to comment
Share on other sites

Don't try to limit resources in creative mode.

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.

Link to comment
Share on other sites

It'd be a lot easier to make the player invincible and give them flight than to try and change the mechanics of the game. I'd recommend just keeping it in survival and buffing the player:

player.capabilities.allowFlying = true;
player.capabilities.disableDamage = true;

 

- Just because things are the way they are doesn't mean they can't be the way you want them to be. Unless they're aspen trees. You can tell they're aspens 'cause the way they are.

Link to comment
Share on other sites

9 minutes ago, AnZaNaMa said:

It'd be a lot easier to make the player invincible and give them flight than to try and change the mechanics of the game. I'd recommend just keeping it in survival and buffing the player:


player.capabilities.allowFlying = true;
player.capabilities.disableDamage = true;

 

Ahh, okay, makes sense. In which part of my code do I set these fields? I tried to do so during initialization but Minecraft.getMinecraft().player seem to be null. Not sure how to access the player properly and when.

Link to comment
Share on other sites

One way you could accomplish this for all players is to use an Event Handler that catches player creation:

public class EventHandlerClass {

	//Called when the player is originally created
	@SubscribeEvent
	public void onPlayerCreate(EntityJoinWorldEvent event) {
		if(event.getEntity() instanceof EntityPlayer) {
			EntityPlayer player = (EntityPlayer)event.getEntity();
			player.capabilities.allowFlying = true;
			player.capabilities.disableDamage = true;
		}
	}

	//Called when the player respawns or changes dimensions. Will ensure that the player maintains its stats.
	@SubscribeEvent
	public void onPlayerClone(PlayerEvent.Clone event) {
		event.getEntityPlayer().capabilities.allowFlying = true;
		event.getEntityPlayer().capabilities.disableDamage = true;
	}
}

and in whatever method handles FMLInitializationEvent, you need to register the class that has an event handler:

public void init(FMLInitializationEvent e) {
	MinecraftForge.EVENT_BUS.register(new EventHandlerClass());
}

 

I've edited this post a couple times, because I haven't been modding for a while, so I forgot some things. Sorry for any confusion

Edited by AnZaNaMa

- Just because things are the way they are doesn't mean they can't be the way you want them to be. Unless they're aspen trees. You can tell they're aspens 'cause the way they are.

Link to comment
Share on other sites

During initialization, there won't even be a World, let alone a player. On top of that, you should not use Minecraft.getMinecraft().player for anything other than rendering, as it exists only on the Client side, and won't work properly.

 

If you don't know much about Forge events, I suggest you check them out. They are probably the most important part of the Forge API and modding the game. You can find a quick tutorial I made here. Also, you can always check the forge docs, which are a great source for information.

- Just because things are the way they are doesn't mean they can't be the way you want them to be. Unless they're aspen trees. You can tell they're aspens 'cause the way they are.

Link to comment
Share on other sites

Just realized you need @SubscribeEvent not @Mod.EventHandler. I'm an idiot and completely forgot that. @Mod.EventHandler is only for FML Lifecycle Events

- Just because things are the way they are doesn't mean they can't be the way you want them to be. Unless they're aspen trees. You can tell they're aspens 'cause the way they are.

Link to comment
Share on other sites

Thanks for all the help. I ended up making an event handler as follows:

 

    @SubscribeEvent
    public void onPlayerLogin(net.minecraftforge.fml.common.gameevent.PlayerEvent.PlayerLoggedInEvent event) {
        event.player.capabilities.isFlying = true;
        event.player.capabilities.disableDamage = true;
        System.out.println("Logged in");
    }

 

using PlayerLoggedInEvent instead of your join world/respawn events. I'm still confused, though -- I know the event is being caught because the printline shows up, but I'm still unable to fly and I still take damage.

Link to comment
Share on other sites

oops sorry, try this.

public void eventHandler(PlayerEvent.PlayerLoggedInEvent event) {
  if(!event.player.getEntityWorld().isRemote()) {
  	event.player.capabilities.allowFlying = true;
  	event.player.capabilities.disableDamage = true;
  	event.player.sendPlayerAbilities();
  }

The if statement just makes sure the code only runs on the server, then the sendPlayerAbilities() forces the server to send updated abilities to the client.

- Just because things are the way they are doesn't mean they can't be the way you want them to be. Unless they're aspen trees. You can tell they're aspens 'cause the way they are.

Link to comment
Share on other sites

Ohh, got it! It works now, thanks so much. Clearly I need to spend a little more time with Forge events and the server/client relationship. 

If you don't mind, can I pick your brain on another thing? I'd like to have this kind of behavior when destroying/placing blocks: on destroy, auto-pickup the dropped block and switch the item being held to that block; on place, stop holding the item (even if you have remaining quantities of that item in your inventory). In essence, I'm trying to be able to emulate "moving" blocks in the world by picking them up into your hand and putting them down. I'm not too familiar with how the inventory/hand are represented client/server-wise -- is this something I can do on the client side only, or do I need to also do something server-side?

Link to comment
Share on other sites

15 minutes ago, aetherean said:

I'd like to have this kind of behavior when destroying/placing blocks...

Again, for what reason? You're completely screwing with what the player expects to happen.

 

What if the item is picked up but the hotbar is full?

What if the player wants to place more than one block at a time?

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.

Link to comment
Share on other sites

8 minutes ago, Draco18s said:

Again, for what reason? You're completely screwing with what the player expects to happen.

 

What if the item is picked up but the hotbar is full?

What if the player wants to place more than one block at a time?

I am creating a world where the only blocks that can be destroyed are my defined blocks, starting with a limited quantity of each in the player's inventory. Effectively there won't be any other blocks that should be able to be added to the player's hotbar.

 

I am also trying to force the player to have to reselect the inventory slot of the block they'd like to place if they wanted to place more than one block at a time. It's more tedious for the player, but the world we're trying to simulate is one where you'd have to pick up a block (either by picking up in the world or selecting explicitly from your inventory) every time you want to place one, instead of blocks magically appearing in your hand.

 

Sorry if this use case seems confusing. For context, we're trying to use this for data collection in an environment that behaves fundamentally differently from Minecraft, but a lot of the elements (e.g. blocks, limited resources, structure building) are similar enough that we'd like to use Minecraft as a base for simulation.

Edited by aetherean
Link to comment
Share on other sites

Well, If that's what you want to do, you'll probably need an EventHandler for whatever event occurs when a player breaks a block and you'll want to cancel the event if it's not one of your blocks. I'm not at my computer right now so I can't say exactly what you'd need. 

- Just because things are the way they are doesn't mean they can't be the way you want them to be. Unless they're aspen trees. You can tell they're aspens 'cause the way they are.

Link to comment
Share on other sites

1 hour ago, aetherean said:

I am creating a world where the only blocks that can be destroyed are my defined blocks, starting with a limited quantity of each in the player's inventory. Effectively there won't be any other blocks that should be able to be added to the player's hotbar.

That's fine, but there's still no reason to screw around with which hotbar slot is selected.

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.

Link to comment
Share on other sites

How are you testing multiplayer? Are you running a dedicated server, or via LAN?

Is the server running the mod?

- Just because things are the way they are doesn't mean they can't be the way you want them to be. Unless they're aspen trees. You can tell they're aspens 'cause the way they are.

Link to comment
Share on other sites

19 minutes ago, AnZaNaMa said:

How are you testing multiplayer? Are you running a dedicated server, or via LAN?

Is the server running the mod?

From what I understand (I'm not in charge of the multiplayer stuff), so far we've been debugging by having two clients on localhost, one of whom joins the server that the other spawns on. In the future, we'll probably want to do this via LAN to support connecting from multiple machines. I don't think there are any plans of using a dedicated server.

 

The mod does run in this setup -- my custom blocks and such work fine.

Edited by aetherean
added details
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.

×
×
  • Create New...

Important Information

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