Jump to content
  • Sign Up
  • 0
AllergenX

Cancel an OnBlockChangingEvent?

Question

I think the goal here is pretty simple, I need to cancel an OnBlockChanging event.

I have tried to set the variable "cancel" to true, but it doesn't seem to cancel the block being placed.
Here's my code, if it is helpful:

using System;
using System.Collections.Generic;
using System.IO;
using MCGalaxy;
using MCGalaxy.Events.ServerEvents;
using MCGalaxy.Events.PlayerEvents;
using MCGalaxy.Blocks.Extended;
using BlockID = System.UInt16;
using System.Reflection;
using MCGalaxy.Tasks;

namespace PluginAllersSurvival
{
	public class AllersSurvival : Plugin
	{
		public override string name { get { return "AllersSurvival"; } }
		public override string MCGalaxy_Version { get { return "1.9.4.9"; } }
		public override string welcome { get { return "Thanks for using my plugin, Aller's Survival!"; } }
		public override string creator { get { return "AllergenX"; } }
		
		public static Dictionary<string, int> blockCounts = new Dictionary<string, int>();

		// Called when this plugin is being loaded (e.g. on server startup)
		public override void Load(bool startup)
		{
			OnBlockChangingEvent.Register(OnBlockChanging, Priority.High);
			Player[] players = PlayerInfo.Online.Items;
			foreach (Player p in players) {
				blockCounts[p.DisplayName] = 0;
			}
		}

		// Called when this plugin is being unloaded (e.g. on server shutdown)
		public override void Unload(bool shutdown)
		{
			OnBlockChangingEvent.Unregister(OnBlockChanging);
		}
		
		public static void OnBlockChanging(Player p, ushort x, ushort y, ushort z, BlockID block, bool placing, ref bool cancel) {
			if (p.Level.Config.MOTD.Contains("+survival")) {
				if (placing == false) {
					blockCounts[p.DisplayName] = blockCounts[p.DisplayName] + 1;
					p.Message("You have " + Convert.ToString(blockCounts[p.DisplayName]) + " blocks.");
				} else {
					if (placing == true) {
						if (blockCounts[p.DisplayName] >= 1) {
							blockCounts[p.DisplayName] = blockCounts[p.DisplayName] - 1;
							p.Message("You have " + Convert.ToString(blockCounts[p.DisplayName]) + " blocks.");
						} else if (blockCounts[p.DisplayName] <= 0) {
							p.Message("You are out of blocks.");
							cancel = true;
							p.Message(Convert.ToString(cancel));
						}
					}
				}
			}	
		}

		// Displays help for or information about this plugin
		public override void Help(Player p) {
			p.Message("Super simple and awesome survival plugin.");
		}
	}
}

Yes, it messages me "You are out of blocks." and yes, it messages me "True".

Share this post


Link to post

2 answers to this question

Recommended Posts

  • 2

The cancel argument only stops the level itself from being edited. The player will still see their change appear, but only on their screen. To fix this, you need to send a block change back to the player to visually undo the block they tried to place or delete. p.SendBlockchange((ushort)x, (ushort)y, (ushort)z, block);

Share this post


Link to post
  • -2
Posted (edited)

You need to use a HandlePlace delegate instead and set that for the level you want the logic to work on using the following code

//You want this to be called when the level is loaded.
//BLOCK_ID is the block id you want to target. To target all simply make a for loop of range [0, 1024) 
level.UpdateBlockHandlers(BLOCK_ID);

//This is an event which takes OnBlockHandlersUpdated delegates, you want to register this in your plugin
//You may want to add a check to make sure this only sets the proper place handler.
void OnBlockHandlersUpdated(Level lvl, BlockID block)
{
  //Ensure if the map this event is called for is the one the plugin is targeting
  //Remove this if you want to target every map.
  if (lvl != pluginLevel) return;
  //You may want to retain the original place handler, so you might use a lambda here to invoke the original place handler.
  //I'm unsure if there is a different method to basically override the placement handler whilst retaining the original logic in place.
  lvl.PlaceHandlers[block] = PlaceHandler;
}

Some other advice to look through Zombie Survival code to see how they did finite blocks.

Edited by icanttellyou

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...