Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Added SignChangeEvent

Signed-off-by: dredhorse <dredhorse@breiden.net>
  • Loading branch information...
commit 6b443717e6199b730302931398d02155a72b2b92 1 parent 6312ba0
Don Redhorse authored July 28, 2012
27  src/main/java/org/spout/vanilla/controller/block/Sign.java
@@ -28,11 +28,15 @@
28 28
 
29 29
 import java.util.HashSet;
30 30
 
  31
+import org.spout.api.Source;
  32
+import org.spout.api.Spout;
  33
+import org.spout.api.event.EventManager;
31 34
 import org.spout.api.geo.cuboid.Block;
32 35
 import org.spout.api.player.Player;
33 36
 
34 37
 import org.spout.vanilla.controller.VanillaBlockController;
35 38
 import org.spout.vanilla.controller.VanillaControllerTypes;
  39
+import org.spout.vanilla.event.block.SignChangeEvent;
36 40
 import org.spout.vanilla.material.VanillaMaterials;
37 41
 import org.spout.vanilla.protocol.msg.UpdateSignMessage;
38 42
 import org.spout.vanilla.util.VanillaNetworkUtil;
@@ -41,6 +45,7 @@
41 45
 	private String[] text = new String[4];
42 46
 	private HashSet<Player> dirty = new HashSet<Player>();
43 47
 	private int range = 20;
  48
+	EventManager eventManager = Spout.getEngine().getEventManager();
44 49
 
45 50
 	public Sign() {
46 51
 		super(VanillaControllerTypes.SIGN, VanillaMaterials.SIGN.getPlacedMaterial());
@@ -92,14 +97,18 @@ public void setRange(int range) {
92 97
 
93 98
 	/**
94 99
 	 * Sets the current lines of this Sign
  100
+	 * @param source which is setting the text lines
95 101
 	 * @param text to set to
96 102
 	 */
97  
-	public void setText(String[] text) throws IllegalArgumentException {
  103
+	public void setText(Source source, String[] text) throws IllegalArgumentException {
98 104
 		if (text.length != 4) {
99 105
 			throw new IllegalArgumentException("The amount of lines have to equal 4");
100 106
 		}
101  
-		this.text = text;
102  
-		update();
  107
+		SignChangeEvent signChangeEvent = eventManager.callEvent(new SignChangeEvent(this, source, text));
  108
+		if (!signChangeEvent.isCancelled()) {
  109
+			this.text = text;
  110
+			update();
  111
+		}
103 112
 	}
104 113
 
105 114
 	/**
@@ -113,12 +122,18 @@ public String getLine(int line) {
113 122
 
114 123
 	/**
115 124
 	 * Sets the line text at a certain line row
  125
+	 * @param source which sets the line
116 126
 	 * @param line row index
117 127
 	 * @param text to set the line to
118 128
 	 */
119  
-	public void setLine(int line, String text) {
120  
-		this.text[line] = text;
121  
-		update();
  129
+	public void setLine(Source source, int line, String text) {
  130
+		String[] temp = this.text;
  131
+		temp[line] = text;
  132
+		SignChangeEvent signChangeEvent = eventManager.callEvent(new SignChangeEvent(this, source, temp));
  133
+		if (!signChangeEvent.isCancelled()) {
  134
+			this.text[line] = text;
  135
+			update();
  136
+		}
122 137
 	}
123 138
 
124 139
 	private void update() {
90  src/main/java/org/spout/vanilla/event/block/SignChangeEvent.java
... ...
@@ -0,0 +1,90 @@
  1
+/*
  2
+ * This file is part of Vanilla.
  3
+ *
  4
+ * Copyright (c) 2011-2012, VanillaDev <http://www.spout.org/>
  5
+ * Vanilla is licensed under the SpoutDev License Version 1.
  6
+ *
  7
+ * Vanilla is free software: you can redistribute it and/or modify
  8
+ * it under the terms of the GNU Lesser General Public License as published by
  9
+ * the Free Software Foundation, either version 3 of the License, or
  10
+ * (at your option) any later version.
  11
+ *
  12
+ * In addition, 180 days after any changes are published, you can use the
  13
+ * software, incorporating those changes, under the terms of the MIT license,
  14
+ * as described in the SpoutDev License Version 1.
  15
+ *
  16
+ * Vanilla is distributed in the hope that it will be useful,
  17
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  19
+ * GNU Lesser General Public License for more details.
  20
+ *
  21
+ * You should have received a copy of the GNU Lesser General Public License,
  22
+ * the MIT license and the SpoutDev License Version 1 along with this program.
  23
+ * If not, see <http://www.gnu.org/licenses/> for the GNU Lesser General Public
  24
+ * License and see <http://www.spout.org/SpoutDevLicenseV1.txt> for the full license,
  25
+ * including the MIT license.
  26
+ */
  27
+package org.spout.vanilla.event.block;
  28
+
  29
+import org.spout.api.Source;
  30
+import org.spout.api.event.Cancellable;
  31
+import org.spout.api.event.Event;
  32
+import org.spout.api.event.HandlerList;
  33
+
  34
+import org.spout.vanilla.controller.block.Sign;
  35
+
  36
+/**
  37
+ * Event which is called when the text on a sign changes
  38
+ */
  39
+public class SignChangeEvent extends Event implements Cancellable {
  40
+	private static HandlerList handlers = new HandlerList();
  41
+	private String[] text;
  42
+	private Sign sign;
  43
+	private Source source;
  44
+
  45
+	public SignChangeEvent(Sign sign, Source source, String[] text) {
  46
+		super();
  47
+		this.sign = sign;
  48
+		this.source = source;
  49
+		this.text = text;
  50
+	}
  51
+
  52
+	/**
  53
+	 * Get the source which changes the text on a sign
  54
+	 * @return source which changes the text on a sign
  55
+	 */
  56
+	public Source getSource() {
  57
+		return source;
  58
+	}
  59
+
  60
+	/**
  61
+	 * Get the sign which text is being changed
  62
+	 * @return sign which is being changed
  63
+	 */
  64
+	public Sign getSign() {
  65
+		return sign;
  66
+	}
  67
+
  68
+	/**
  69
+	 * Get the text which should be placed on the sign. The text being returned isn't
  70
+	 * necessary the text entered by the Source if the source only changed one line.
  71
+	 * @return text which is placed on the sign 1 - 4 lines
  72
+	 */
  73
+	public String[] getText() {
  74
+		return text;
  75
+	}
  76
+
  77
+	@Override
  78
+	public void setCancelled(boolean cancelled) {
  79
+		super.setCancelled(cancelled);
  80
+	}
  81
+
  82
+	public static HandlerList getHandlerList() {
  83
+		return handlers;
  84
+	}
  85
+
  86
+	@Override
  87
+	public HandlerList getHandlers() {
  88
+		return handlers;
  89
+	}
  90
+}
2  src/main/java/org/spout/vanilla/protocol/handler/UpdateSignHandler.java
@@ -52,6 +52,6 @@ public void handleServer(Session session, Player player, UpdateSignMessage messa
52 52
 		}
53 53
 
54 54
 		Sign sign = (Sign) controller;
55  
-		sign.setText(text);
  55
+		sign.setText(player, text);
56 56
 	}
57 57
 }

0 notes on commit 6b44371

Please sign in to comment.
Something went wrong with that request. Please try again.