Jump to content

[1.11.2] Removing blocks when picked from the world


Recommended Posts

Posted

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.

Posted

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.

Posted

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"?

Posted (edited)

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
Posted

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.

Posted

Is there a reason I shouldn't? Does it break things fundamentally somehow?

I need an environment where players can freely fly and build structures, but only using my predefined blocks and with a limited quantity. If I should use a different mode, how would I achieve this?

Posted

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.

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

Posted (edited)

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.

Posted

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.

Posted

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.

Posted

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.

Posted

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.

Posted

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?

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

Posted (edited)
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
Posted

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.

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

Posted

@AnZaNaMa going back to debugging the flying/invulnerability code upon agent startup, we realized that these player capabilities aren't enabled when in a multiplayer setting (though it seems the event handler still fires). Is sendPlayerAbilities() single-player only? Or is there some extension to this code I should add?

Posted

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.

Posted (edited)
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
Posted

Well, as long as both clients are running the mod, I can't see why you would have any issues with it. Maybe @Draco18s has an idea?

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

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



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • Also check the worldsave / serverconfig folder If there is no such file, make a test without this mod  
    • Hi, I've been having trouble trying to use forge as it shows a black screen when I open the game, but I can still interact with it and hear the music.  I've done all of the step by steps and most common fixes like updating drivers, keeping up to date with Java, deleting and reinstalling minecraft, restarting my computer MANY times, even smaller things like splash.properties (I didn't have that file so I added it and set it to false thinking it would do something, definitely not) and making sure to prioritize my rtx 3070 in the settings but with no luck. Minecraft works as intended when I uninstall forge and I also don't have any mods currently, it just gives me this issue when I install forge. I also increased the ram usage, made sure my hardware isn't full or anything, and even changed the resolution in hopes it would fix things. I checked my antivirus and firewall but that isn't the issue either. Trust me, I've done everything I can think of. For some reason the black screen does flicker a little into the main menu, but obviously unplayable. I couldn't even make my way to the settings with how little it flickered. I'm not sure if it flickered randomly or if it was because I was messing around moving and clicking a bunch, I didn't really test it that much.  
    • I've had a really weird issue recently,  I wanted to add the Depper and Darker mod on my dedicated server (MC 1.21 with Fabric 0.16.9, hosted on nitroserv.com) but whenever I do add the mod the sever stops doing anything after listing the mods, and I get no crash or error or anything, just a stuck server. Here's a normal log of the server booting up: https://pastebin.com/JipFF2Eh and here's the log of the server doing the weird thing: https://pastebin.com/W4JBh3eX I just don't understand it. I've tried removing other mods (somewhat randomly) but deeper and darker still breaks my server whenever I add it. NitroServ support staff is about as confused as I am and I've had no response from the Deeper and Darker support staff... Now I know this is the Forge support not the Fabric support but I'm just trying to know if anyone has any kind of idea to fix this (aside from not using the mod obviously) Also I still have a bunch of errors and warnings whenever the server does start properly, are there any of them I should be worried about?
  • Topics

×
×
  • Create New...

Important Information

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