Jump to content

Have a block change when a player sleeps


darthvader45

Recommended Posts

I remember an old 1.6.4 mod called ChristmasCraft, made by newthead and once updated to 1.7.10 by Zuxelas.  The stocking block included in the mod changes to have a present in it when the player sleeps, and the present could be retrieved by a right-click.

 

How can I transfer this code into a 1.12.2 version?

 

Here's the code used in the mod's 1.7.10 version:

@SubscribeEvent
	public void onWorldTick(WorldTickEvent event)
	{
		if (event.phase == Phase.START)
		{
			if (!ChristmasCraft.isChristmasTime())
			{
				return;
			}
			boolean isChristmasDay = ChristmasCraft.isChristmasDay();
			WorldServer world = (WorldServer)event.world;
			if (world.areAllPlayersAsleep())
			{
				Object[] tiles = world.loadedTileEntityList.toArray();
				for (int i = 0; i < tiles.length; i++)
				{
					TileEntity tile = (TileEntity)tiles[i];
					Block block = world.getBlock(tile.xCoord, tile.yCoord, tile.zCoord);
					if (block == ChristmasCraft.blockStocking)
					{
						ItemStack present = ChristmasCraft.getRandomStockingGift();
						if (((TileEntityStocking)tile).getContents() == null)
						{
							((TileEntityStocking)tile).setContents(present);
						}
					}
					if (isChristmasDay && block == ChristmasCraft.blockTreestand)
					{
						generatePresentsAroundTree(world, tile.xCoord, tile.yCoord, tile.zCoord);
					}
				}
			}
		}
	}

 

BlockStocking:

@Override
	public boolean onBlockActivated(World world, int i, int j, int k, EntityPlayer entityplayer, int side, float f1, float f2, float f3)
	{
		ItemStack contents = null;
		TileEntityStocking tile = (TileEntityStocking)world.getTileEntity(i, j, k);
		if (tile != null && tile.getContents() != null)
		{
			contents = tile.getContents();
		}
		if (entityplayer.getCurrentEquippedItem() != null && contents == null)
		{
			ItemStack heldItem = entityplayer.getCurrentEquippedItem();
			if (Block.getBlockFromItem(heldItem.getItem()) == ChristmasCraft.blockPresent)
			{
				ItemStack newContents = heldItem.copy();
				newContents.stackSize = 1;
				tile.setContents(newContents);
				heldItem.stackSize -= 1;
				return true;
			}
		}
		else
		{
			dropStockingContents(world, i, j, k);
			return true;
		}
		return false;
	}

 

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.