summaryrefslogtreecommitdiff
path: root/YWD/src/main/java/fyresmodjam/tileentities/TileEntityTrap.java
blob: 9122f38b883f431e56b44219753073f103532c16 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package fyresmodjam.tileentities;

import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import fyresmodjam.ModjamMod;
import fyresmodjam.blessings.BlessingUtils;
import fyresmodjam.handlers.NewPacketHandler;
import net.minecraft.client.Minecraft;
import net.minecraft.client.renderer.tileentity.TileEntityRendererDispatcher;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.network.NetworkManager;
import net.minecraft.network.Packet;
import net.minecraft.network.play.server.S35PacketUpdateTileEntity;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.AxisAlignedBB;

public class TileEntityTrap extends TileEntity {

	public static String[] settings = { "invisible to and damages all but player",
			"visible to all and damages all but player", "visible to all and only damages mobs", "decorative" };

	public String placedBy = null;
	public int setting = 0;

	public TileEntityTrap() {
	}

	@Override
	public void updateEntity() {
		super.updateEntity();

		if (worldObj.isRemote) {
			spawnParticles();
		}
	}

	@Override
	public void writeToNBT(NBTTagCompound par1NBTTagCompound) {
		super.writeToNBT(par1NBTTagCompound);

		if (placedBy != null) {
			par1NBTTagCompound.setString("PlacedBy", placedBy);
		}
		par1NBTTagCompound.setInteger("Setting", setting % settings.length);
	}

	@Override
	public void readFromNBT(NBTTagCompound par1NBTTagCompound) {
		super.readFromNBT(par1NBTTagCompound);

		if (par1NBTTagCompound.hasKey("PlacedBy")) {
			placedBy = par1NBTTagCompound.getString("PlacedBy");
		} else {
			placedBy = null;
		}

		if (par1NBTTagCompound.hasKey("Setting")) {
			setting = par1NBTTagCompound.getInteger("Setting") % settings.length;
		}
	}

	@Override
	public Packet getDescriptionPacket() {
		NBTTagCompound tag = new NBTTagCompound();
		writeToNBT(tag);
		return new S35PacketUpdateTileEntity(xCoord, yCoord, zCoord, 1, tag);
	}

	@Override
	public void onDataPacket(NetworkManager net, S35PacketUpdateTileEntity pkt) {
		readFromNBT(pkt.func_148857_g());
	}

	@Override
	@SideOnly(Side.CLIENT)
	public AxisAlignedBB getRenderBoundingBox() {
		return INFINITE_EXTENT_AABB;
	}

	@Override
	@SideOnly(Side.CLIENT)
	public double getMaxRenderDistanceSquared() {
		EntityPlayer player = Minecraft.getMinecraft().thePlayer;
		return (player != null && (player.getCommandSenderName().equals(placedBy) || setting != 0)) ? 4096.0F : 36.0F;
	}

	@SideOnly(Side.CLIENT)
	public void spawnParticles() {
		EntityPlayer player = Minecraft.getMinecraft().thePlayer;
		int type = worldObj.getBlockMetadata(xCoord, yCoord, zCoord);

		if (player != null && (!NewPacketHandler.trapsDisabled || placedBy != null)
				&& (player.getCommandSenderName().equals(placedBy) || player.isSneaking() || setting != 0
						|| (BlessingUtils.hasBlessing(player, "MarkScouting")))
				&& getDistanceFrom(TileEntityRendererDispatcher.staticPlayerX,
						TileEntityRendererDispatcher.staticPlayerY,
						TileEntityRendererDispatcher.staticPlayerZ) < (player.getCommandSenderName().equals(placedBy)
								? 4096
								: 36.0F)) {
			if (type == 1) {
				if (ModjamMod.r.nextInt(5) == 0) {
					worldObj.spawnParticle("smoke", xCoord + 0.5F, yCoord + 0.175F, zCoord + 0.5F,
							(ModjamMod.r.nextFloat() - 0.5F) / 16, ModjamMod.r.nextFloat() / 16,
							(ModjamMod.r.nextFloat() - 0.5F) / 16);
				}
				worldObj.spawnParticle("flame", xCoord + 0.5F, yCoord + 0.175F, zCoord + 0.5F, 0.0F, 0.0F, 0.0F);
			} else if (type == 2) {
				for (int i = 0; i < 3; i++) {
					worldObj.spawnParticle("smoke", xCoord + 0.5F, yCoord + 0.175F, zCoord + 0.5F,
							(ModjamMod.r.nextFloat() - 0.5F) / 16, ModjamMod.r.nextFloat() / 16,
							(ModjamMod.r.nextFloat() - 0.5F) / 16);
				}
			}
		}
	}
}