Updating block metadata
#1
Is it possible to update block metadata?
I have been searching for an alternative to MonsterBlocks: http://dev.bukkit.org/bukkit-plugins/monster-blocks/

I, of course, was not able to find anything, so I decided to code my own alternative. I am not sure how to read and change block metadata. Is it even possible?
Reply
Thanks given by:
#2
Use cWorld:SetBlockMeta(PosX, PosY, PosZ, Meta) for that. You can also use a Vector3i object instead of the 3 position numbers.
Reply
Thanks given by:
#3
Another problem: When I do:
local meta = Player:GetWorld():GetBlockMeta(BlockX, BlockY, BlockZ)
and then send myself a message:
Player:SendMessage(meta)
It prints two lines. I only want one. What even is the other line?
When I send a HOOK_PLAYER_LEFT_CLICK hook to the function, it prints the meta id to the second line and when I send a HOOK_PLAYER_RIGHT_CLICK, it prints it on the first line...
The additional line is always 1.
Reply
Thanks given by:
#4
Could you show us our code?
Reply
Thanks given by:
#5
(01-21-2016, 06:44 AM)NiLSPACE Wrote: Could you show us our code?

The mcBlockRespawn function gets called by a HOOK_PLAYER_BREAKING_BLOCK (so i can prevent block breaking in creative), otherwise the mcLeft function is called by HOOK_PLAYER_LEFT_CLICK. This is because HOOK_PLAYER_LEFT_CLICK is not registered in creative.

Here are the files if you want to try it yourself: http://a.pomf.cat/xvwliy.7z

mcMetaTool = 280
mcBiomeTool = 369

function mcLeft(Player, BlockX, BlockY, BlockZ, BlockFace, Action)
	if (Player:GetEquippedItem().m_ItemType == mcMetaTool) then
		mcMetaLeft(Player, BlockX, BlockY, BlockZ, BlockFace, Action)
	elseif (Player:GetEquippedItem().m_ItemType == mcBiomeTool) then
		mcBiomeLeft(Player, BlockX, BlockY, BlockZ, BlockFace, Action)
	end
end

function mcRight(Player, BlockX, BlockY, BlockZ)
	if (Player:GetEquippedItem().m_ItemType == mcMetaTool) then
		mcMetaRight(Player, BlockX, BlockY, BlockZ)
	elseif (Player:GetEquippedItem().m_ItemType == mcBiomeTool) then
		mcBiomeRight(Player, BlockX, BlockY, BlockZ)
	end
end

--

function mcMetaLeft(Player, BlockX, BlockY, BlockZ, BlockFace, Action)
	if (Action == 1) then
		local meta = Player:GetWorld():GetBlockMeta(BlockX, BlockY, BlockZ)
		if (meta == 0) then
			meta = 16
		else
			meta = meta - 1
		end
		Player:SendMessage(meta)
		Player:GetWorld():SetBlockMeta(BlockX, BlockY, BlockZ, meta)
	end
end

function mcMetaRight(Player, BlockX, BlockY, BlockZ)
	local meta = Player:GetWorld():GetBlockMeta(BlockX, BlockY, BlockZ)
	if (meta == 16) then
		meta = 0
	else
		meta = meta + 1
	end
	Player:SendMessage(meta)
	Player:GetWorld():SetBlockMeta(BlockX, BlockY, BlockZ, meta)
end

function mcBiomeLeft(Player, BlockX, BlockY, BlockZ, BlockFace, Action)
	if (Action == 1) then
		local biome = Player:GetWorld():GetBiomeAt(BlockX, BlockZ)
		biome = biome - 1
		Player:SendMessage(biome)
		Player:GetWorld():SetAreaBiome(BlockX, BlockX, BlockZ, BlockZ, biome)
	end
end

function mcBiomeRight(Player, BlockX, BlockY, BlockZ)
	local biome = Player:GetWorld():GetBiomeAt(BlockX, BlockZ)
	biome = biome + 1
	Player:SendMessage(biome)
	Player:GetWorld():SetAreaBiome(BlockX, BlockX, BlockZ, BlockZ, biome)
end

--

function mcBlockRespawn(Player, BlockX, BlockY, BlockZ, BlockFace, Action)
	if (Player:GetEquippedItem().m_ItemType == mcMetaTool) or (Player:GetEquippedItem().m_ItemType == mcBiomeTool) then
		if (Player:GetGameMode() == 1) then
			mcLeft(Player, BlockX, BlockY, BlockZ, BlockFace, 1)
		end
		return true
	end
end
Reply
Thanks given by:
#6
Strange, the left click should be called normally in creative. I'm afraid I can't test this at the moment though, so I'll get back to this later.
Reply
Thanks given by:
#7
(01-21-2016, 05:37 PM)NiLSPACE Wrote: Strange, the left click should be called normally in creative. I'm afraid I can't test this at the moment though, so I'll get back to this later.

Interesting indeed, but that is not the main issue after all, since I can hack around it.
The double meta thing is a different problem entirely.

I have decided to ignore the biome changing for now and fix the meta changing function. Right clicking works well except that it sends me two lines of numbers (one is the real metadata, the other is who knows what). Left clicking the block is just broken because of the two meta numbers. That, as a result, does not let the mcBlockRespawn function prevent the block from being removed.

Another issue is that right-clicking does automatically cycle back to 0 if it reaches meta 16 and 1 is added to it by the function. This does not work the other way when left-clicking and the block simple gets destroyed (in creative).
Reply
Thanks given by:
#8
What do you mean with 'two meta numbers'?

The right-click is called twice because Minecraft sends two packets.

Add this check in front of the hook:
if (BlockFace == BLOCK_FACE_NONE) then
   return false
end
Reply
Thanks given by:
#9
(01-21-2016, 08:51 PM)NiLSPACE Wrote: Add this check in front of the hook:
if (BlockFace == BLOCK_FACE_NONE) then
   return false
end

Where exactly?

(01-21-2016, 08:51 PM)NiLSPACE Wrote: The right-click is called twice because Minecraft sends two packets.

If the right-click function was called twice, it would increase the number by 2 on one click. Instead it returns an entirely new number.
Here is a screenshot with all the scenarios:
http://i.imgur.com/9kVqPYU.png
The numbers returned in chat are simply the meta variable...
Reply
Thanks given by:
#10
Add the check for the right click hook.

Also, I believe you should check if the meta is 15 instead of 16. The meta starts from 0 until 15. For the same reason you should also set the left click to 15 instead of 16 when the meta is 0.
Reply
Thanks given by:




Users browsing this thread: 1 Guest(s)