Ey

Ey

Postby _Retaliate_ » 17 Feb 2014, 10:43

so, I got a problem with a command I'm making, basically I'm making it so you can equip a block that you have in your "inventory" and you'll place that block until you don't have any left. I am having problems with detecting when the player places something instead of destroying something because changing a block can mean both, and when they run out of blocks, it's supposed to remove the block from their inventory and stop placing it, however it doesn't do that, it just keeps it there with 0 blocks of that type stored, and keeps on letting them place that block. Also, it will only place 1 block of it and then it will stop
This is the code for them equipping it, I don't think anything is wrong with that
Code: Select all
            if(pe.equipped == item) {
               Player.SendMessage(p, "That block is already equipped.");
               return;
            } else {
               pe.equipped = item;
               Player.SendMessage(p, Block.Name(item) + " equipped.");
               p.Blockchange += new Player.BlockchangeEventHandler(this.Blockchange1);
            }

And this is where I think the error is, the Blockchange1() void.
Code: Select all
        public void Blockchange1(Player p, ushort x, ushort y, ushort z, byte type)
        {
         p.ClearBlockchange();
            byte tile = type;
         PlayerExtra pe = PlayerExtra.Find(p);
         if(pe == null) {
            pe = new PlayerExtra(p);
         }
         if(pe.equipped != (byte)255) {
            if(tile==Block.Byte("air")) {
               p.level.Blockchange(p, x, y, z, tile);
               return;
            }
            int value;
            if(pe.inventory.TryGetValue(pe.equipped, out value) == false) {
               pe.inventory.Remove(pe.equipped);
               pe.equipped = (byte)255;
               p.Blockchange -= new Player.BlockchangeEventHandler(this.Blockchange1);
               return;
            } else if (value == 0) {
               pe.inventory.Remove(pe.equipped);
               pe.equipped = (byte)255;
               p.Blockchange -= new Player.BlockchangeEventHandler(this.Blockchange1);
               return;
            }
            p.level.Blockchange(p, x, y, z, pe.equipped);
            pe.inventory.Remove(pe.equipped);
            pe.inventory.Add(pe.equipped, value - 1);
            Player.SendMessage(p, "Placed 1 block of " + Block.Name(pe.equipped) + ".");
         }
        }

I wouldn't be surprised if I'm having a derp moment. I would appreciate any feedback you can give me.
Image
_Retaliate_
 
Posts: 68
Joined: 26 Sep 2013, 11:16

Re: Ey

Postby dzienny » 17 Feb 2014, 15:32

It's a good question. I had a similar problem some time ago. So, I added a new event.
Code: Select all
Player.Blockchange2

It requires different signature:
Code: Select all
public delegate void BlockchangeEventHandler2(Player p, ushort x, ushort y, ushort z, byte type, byte action);


Here's a very quick review of your code:
http://www.coderemarks.com/review/wmEVUIvzIZv55xuv


I have very little time this week, so I may answer more questions the next week.

Anyway, the code looks promising :)
User avatar
dzienny
Administrator
 
Posts: 1181
Joined: 23 Jan 2011, 14:27


Return to Help in Coding

Who is online

Users browsing this forum: No registered users and 1 guest

cron