Advertisement
Guest User

Untitled

a guest
Sep 16th, 2017
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.77 KB | None | 0 0
  1. package com.minecolonies.coremod.tileentities;
  2.  
  3. import net.minecraft.command.CommandException;
  4. import net.minecraft.command.CommandResultStats;
  5. import net.minecraft.command.ICommandSender;
  6. import net.minecraft.entity.Entity;
  7. import net.minecraft.entity.player.EntityPlayer;
  8. import net.minecraft.nbt.NBTTagCompound;
  9. import net.minecraft.network.play.server.SPacketUpdateTileEntity;
  10. import net.minecraft.server.MinecraftServer;
  11. import net.minecraft.tileentity.TileEntity;
  12. import net.minecraft.util.math.BlockPos;
  13. import net.minecraft.util.math.Vec3d;
  14. import net.minecraft.util.text.ITextComponent;
  15. import net.minecraft.util.text.Style;
  16. import net.minecraft.util.text.TextComponentString;
  17. import net.minecraft.util.text.TextComponentUtils;
  18. import net.minecraft.util.text.event.ClickEvent;
  19. import net.minecraft.world.World;
  20. import net.minecraftforge.fml.relauncher.Side;
  21. import net.minecraftforge.fml.relauncher.SideOnly;
  22.  
  23. import javax.annotation.Nullable;
  24.  
  25. public class TileEntityInfoPoster extends TileEntity
  26. {
  27. public final ITextComponent[] signText =
  28. new ITextComponent[] {new TextComponentString(""), new TextComponentString(""), new TextComponentString(""), new TextComponentString("")};
  29.  
  30. /**
  31. * The index of the line currently being edited. Only used on client side, but defined on both. Note this is only
  32. * really used when the > < are going to be visible.
  33. */
  34. public int lineBeingEdited = -1;
  35. private boolean isEditable = true;
  36. private EntityPlayer player;
  37. private final CommandResultStats stats = new CommandResultStats();
  38.  
  39. public NBTTagCompound writeToNBT(NBTTagCompound compound)
  40. {
  41. super.writeToNBT(compound);
  42.  
  43. for (int i = 0; i < 4; ++i)
  44. {
  45. String s = ITextComponent.Serializer.componentToJson(this.signText[i]);
  46. compound.setString("Text" + (i + 1), s);
  47. }
  48.  
  49. this.stats.writeStatsToNBT(compound);
  50. return compound;
  51. }
  52.  
  53. protected void setWorldCreate(World worldIn)
  54. {
  55. this.setWorldObj(worldIn);
  56. }
  57.  
  58. public void readFromNBT(NBTTagCompound compound)
  59. {
  60. this.isEditable = false;
  61. super.readFromNBT(compound);
  62. ICommandSender icommandsender = new ICommandSender()
  63. {
  64. /**
  65. * Get the name of this object. For players this returns their username
  66. */
  67. public String getName()
  68. {
  69. return "Sign";
  70. }
  71.  
  72. /**
  73. * Get the formatted ChatComponent that will be used for the sender's username in chat
  74. */
  75. public ITextComponent getDisplayName()
  76. {
  77. return new TextComponentString(this.getName());
  78. }
  79.  
  80. /**
  81. * Send a chat message to the CommandSender
  82. */
  83. public void addChatMessage(ITextComponent component)
  84. {
  85. }
  86.  
  87. /**
  88. * Returns {@code true} if the CommandSender is allowed to execute the command, {@code false} if not
  89. */
  90. public boolean canCommandSenderUseCommand(int permLevel, String commandName)
  91. {
  92. return permLevel <= 2; //Forge: Fixes MC-75630 - Exploit with signs and command blocks
  93. }
  94.  
  95. /**
  96. * Get the position in the world. <b>{@code null} is not allowed!</b> If you are not an entity in the world,
  97. * return the coordinates 0, 0, 0
  98. */
  99. public BlockPos getPosition()
  100. {
  101. return TileEntityInfoPoster.this.pos;
  102. }
  103.  
  104. /**
  105. * Get the position vector. <b>{@code null} is not allowed!</b> If you are not an entity in the world,
  106. * return 0.0D, 0.0D, 0.0D
  107. */
  108. public Vec3d getPositionVector()
  109. {
  110. return new Vec3d((double) TileEntityInfoPoster.this.pos.getX() + 0.5D,
  111. (double) TileEntityInfoPoster.this.pos.getY() + 0.5D,
  112. (double) TileEntityInfoPoster.this.pos.getZ() + 0.5D);
  113. }
  114.  
  115. /**
  116. * Get the world, if available. <b>{@code null} is not allowed!</b> If you are not an entity in the world,
  117. * return the overworld
  118. */
  119. public World getEntityWorld()
  120. {
  121. return TileEntityInfoPoster.this.worldObj;
  122. }
  123.  
  124. /**
  125. * Returns the entity associated with the command sender. MAY BE NULL!
  126. */
  127. public Entity getCommandSenderEntity()
  128. {
  129. return null;
  130. }
  131.  
  132. /**
  133. * Returns true if the command sender should be sent feedback about executed commands
  134. */
  135. public boolean sendCommandFeedback()
  136. {
  137. return false;
  138. }
  139.  
  140. public void setCommandStat(CommandResultStats.Type type, int amount)
  141. {
  142. }
  143.  
  144. /**
  145. * Get the Minecraft server instance
  146. */
  147. public MinecraftServer getServer()
  148. {
  149. return TileEntityInfoPoster.this.worldObj.getMinecraftServer();
  150. }
  151. };
  152.  
  153. for (int i = 0; i < 4; ++i)
  154. {
  155. String s = compound.getString("Text" + (i + 1));
  156. ITextComponent itextcomponent = ITextComponent.Serializer.jsonToComponent(s);
  157.  
  158. try
  159. {
  160. this.signText[i] = TextComponentUtils.processComponent(icommandsender, itextcomponent, (Entity) null);
  161. }
  162. catch (CommandException var7)
  163. {
  164. this.signText[i] = itextcomponent;
  165. }
  166. }
  167.  
  168. this.stats.readStatsFromNBT(compound);
  169. }
  170.  
  171. @Nullable
  172. public SPacketUpdateTileEntity getUpdatePacket()
  173. {
  174. return new SPacketUpdateTileEntity(this.pos, 9, this.getUpdateTag());
  175. }
  176.  
  177. public NBTTagCompound getUpdateTag()
  178. {
  179. return this.writeToNBT(new NBTTagCompound());
  180. }
  181.  
  182. public boolean onlyOpsCanSetNbt()
  183. {
  184. return true;
  185. }
  186.  
  187. public boolean getIsEditable()
  188. {
  189. return this.isEditable;
  190. }
  191.  
  192. /**
  193. * Sets the sign's isEditable flag to the specified parameter.
  194. */
  195. @SideOnly(Side.CLIENT)
  196. public void setEditable(boolean isEditableIn)
  197. {
  198. this.isEditable = isEditableIn;
  199.  
  200. if (!isEditableIn)
  201. {
  202. this.player = null;
  203. }
  204. }
  205.  
  206. public void setPlayer(EntityPlayer playerIn)
  207. {
  208. this.player = playerIn;
  209. }
  210.  
  211. public EntityPlayer getPlayer()
  212. {
  213. return this.player;
  214. }
  215.  
  216. public boolean executeCommand(final EntityPlayer playerIn)
  217. {
  218. ICommandSender icommandsender = new ICommandSender()
  219. {
  220. /**
  221. * Get the name of this object. For players this returns their username
  222. */
  223. public String getName()
  224. {
  225. return playerIn.getName();
  226. }
  227.  
  228. /**
  229. * Get the formatted ChatComponent that will be used for the sender's username in chat
  230. */
  231. public ITextComponent getDisplayName()
  232. {
  233. return playerIn.getDisplayName();
  234. }
  235.  
  236. /**
  237. * Send a chat message to the CommandSender
  238. */
  239. public void addChatMessage(ITextComponent component)
  240. {
  241. }
  242.  
  243. /**
  244. * Returns {@code true} if the CommandSender is allowed to execute the command, {@code false} if not
  245. */
  246. public boolean canCommandSenderUseCommand(int permLevel, String commandName)
  247. {
  248. return permLevel <= 2;
  249. }
  250.  
  251. /**
  252. * Get the position in the world. <b>{@code null} is not allowed!</b> If you are not an entity in the world,
  253. * return the coordinates 0, 0, 0
  254. */
  255. public BlockPos getPosition()
  256. {
  257. return TileEntityInfoPoster.this.pos;
  258. }
  259.  
  260. /**
  261. * Get the position vector. <b>{@code null} is not allowed!</b> If you are not an entity in the world,
  262. * return 0.0D, 0.0D, 0.0D
  263. */
  264. public Vec3d getPositionVector()
  265. {
  266. return new Vec3d((double) TileEntityInfoPoster.this.pos.getX() + 0.5D,
  267. (double) TileEntityInfoPoster.this.pos.getY() + 0.5D,
  268. (double) TileEntityInfoPoster.this.pos.getZ() + 0.5D);
  269. }
  270.  
  271. /**
  272. * Get the world, if available. <b>{@code null} is not allowed!</b> If you are not an entity in the world,
  273. * return the overworld
  274. */
  275. public World getEntityWorld()
  276. {
  277. return playerIn.getEntityWorld();
  278. }
  279.  
  280. /**
  281. * Returns the entity associated with the command sender. MAY BE NULL!
  282. */
  283. public Entity getCommandSenderEntity()
  284. {
  285. return playerIn;
  286. }
  287.  
  288. /**
  289. * Returns true if the command sender should be sent feedback about executed commands
  290. */
  291. public boolean sendCommandFeedback()
  292. {
  293. return false;
  294. }
  295.  
  296. public void setCommandStat(CommandResultStats.Type type, int amount)
  297. {
  298. if (TileEntityInfoPoster.this.worldObj != null && !TileEntityInfoPoster.this.worldObj.isRemote)
  299. {
  300. TileEntityInfoPoster.this.stats.setCommandStatForSender(TileEntityInfoPoster.this.worldObj.getMinecraftServer(), this, type, amount);
  301. }
  302. }
  303.  
  304. /**
  305. * Get the Minecraft server instance
  306. */
  307. public MinecraftServer getServer()
  308. {
  309. return playerIn.getServer();
  310. }
  311. };
  312.  
  313. for (ITextComponent itextcomponent : this.signText)
  314. {
  315. Style style = itextcomponent == null ? null : itextcomponent.getStyle();
  316.  
  317. if (style != null && style.getClickEvent() != null)
  318. {
  319. ClickEvent clickevent = style.getClickEvent();
  320.  
  321. if (clickevent.getAction() == ClickEvent.Action.RUN_COMMAND)
  322. {
  323. playerIn.getServer().getCommandManager().executeCommand(icommandsender, clickevent.getValue());
  324. }
  325. }
  326. }
  327.  
  328. return true;
  329. }
  330.  
  331. public CommandResultStats getStats()
  332. {
  333. return this.stats;
  334. }
  335. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement