Browse Source

Initial commit

Ian Adam Naval 5 years ago
commit
7cf3bd1b8c

+ 7
- 0
.classpath View File

@@ -0,0 +1,7 @@
1
+<?xml version="1.0" encoding="UTF-8"?>
2
+<classpath>
3
+	<classpathentry kind="src" path="src"/>
4
+	<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6"/>
5
+	<classpathentry kind="lib" path="/home/ian/Desktop/minecraft server/craftbukkit-0.0.1-SNAPSHOT.jar"/>
6
+	<classpathentry kind="output" path="bin"/>
7
+</classpath>

+ 1
- 0
.gitignore View File

@@ -0,0 +1 @@
1
+bin/

+ 17
- 0
.project View File

@@ -0,0 +1,17 @@
1
+<?xml version="1.0" encoding="UTF-8"?>
2
+<projectDescription>
3
+	<name>NoTNT</name>
4
+	<comment></comment>
5
+	<projects>
6
+	</projects>
7
+	<buildSpec>
8
+		<buildCommand>
9
+			<name>org.eclipse.jdt.core.javabuilder</name>
10
+			<arguments>
11
+			</arguments>
12
+		</buildCommand>
13
+	</buildSpec>
14
+	<natures>
15
+		<nature>org.eclipse.jdt.core.javanature</nature>
16
+	</natures>
17
+</projectDescription>

+ 12
- 0
.settings/org.eclipse.jdt.core.prefs View File

@@ -0,0 +1,12 @@
1
+#Sat Jul 02 15:06:31 EDT 2011
2
+eclipse.preferences.version=1
3
+org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
4
+org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.6
5
+org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
6
+org.eclipse.jdt.core.compiler.compliance=1.6
7
+org.eclipse.jdt.core.compiler.debug.lineNumber=generate
8
+org.eclipse.jdt.core.compiler.debug.localVariable=generate
9
+org.eclipse.jdt.core.compiler.debug.sourceFile=generate
10
+org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
11
+org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
12
+org.eclipse.jdt.core.compiler.source=1.6

+ 7
- 0
plugin.yml View File

@@ -0,0 +1,7 @@
1
+name: No TNT
2
+main: com.ianonavy.notnt.NoTNT
3
+version: 0.2
4
+website: http://ianonavy.freeiz.com
5
+description: Absolutely bans TNT throughout the server and sends users a configurable message. Works even with WorldEdit.
6
+authors: [ianonavy]
7
+commands:

+ 123
- 0
src/com/ianonavy/notnt/NoTNT.java View File

@@ -0,0 +1,123 @@
1
+package com.ianonavy.notnt;
2
+
3
+import java.io.File;
4
+import java.util.logging.Logger;
5
+
6
+import org.bukkit.event.Event;
7
+import org.bukkit.plugin.PluginDescriptionFile;
8
+import org.bukkit.plugin.PluginManager;
9
+import org.bukkit.plugin.java.JavaPlugin;
10
+import org.bukkit.util.config.Configuration;
11
+
12
+/**
13
+ * Plugin for Minecraft Bukkit servers that bans the placing of TNT throughout the server. 
14
+ * 
15
+ * @author ianonavy
16
+ * @version 0.2
17
+ */
18
+public class NoTNT extends JavaPlugin {
19
+
20
+	// Get the logger
21
+	public final Logger logger = Logger.getLogger("Minecraft");
22
+	
23
+	// Instance fields
24
+	public NoTNTExplosionListener explosionListener;
25
+	private String warning;
26
+	
27
+	// Constants	
28
+	private final String DEFAULT_MESSAGE = "WARNING: TNT attempted to explode at {x}, {y}, {z}. Players nearby: {playerlist}";
29
+	
30
+	/**
31
+	 * Constrcutor for the No TNT plugin.
32
+	 */
33
+	public NoTNT() {
34
+		explosionListener = new NoTNTExplosionListener(this);
35
+	}
36
+	
37
+	/**
38
+	 * Called when the plugin is disabled.
39
+	 */
40
+	@Override
41
+	public void onDisable() {
42
+		this.logger.info("No TNT disabled.");		
43
+	}
44
+
45
+	/**
46
+	 * Called when the plugin is enabled.
47
+	 */
48
+	@Override
49
+	public void onEnable() {
50
+		PluginManager pm = getServer().getPluginManager();
51
+		pm.registerEvent(Event.Type.ENTITY_EXPLODE, this.explosionListener, Event.Priority.High, this);
52
+		PluginDescriptionFile pdfFile = this.getDescription();
53
+		loadConfiguration();
54
+		this.logger.info(pdfFile.getName() + " version " + pdfFile.getVersion() + " enabled.");
55
+	}
56
+	
57
+	/**
58
+	 * Loads the configuration files. If they are missing, it creates them with the default files. 
59
+	 */
60
+	public void loadConfiguration() {
61
+		// Load the config as a Java file.
62
+		String directory = getDataFolder().getPath();
63
+		File configYML = new File(directory + File.separator + "config.yml"); 
64
+		
65
+		// Recreate the directory just in case it got deleted or for when the plugin is
66
+		// first installed.
67
+		new File(directory).mkdir();
68
+		
69
+		// If the config file is missing...
70
+		if (!configYML.exists()) {
71
+			
72
+			// ...create the file and catch any errors.
73
+			try {
74
+				configYML.createNewFile();
75
+				this.logger.info("No TNT: config.yml file missing! Generating new one...");
76
+				
77
+				// Set the message to the   
78
+				getConfiguration().setProperty("notnt.warning", DEFAULT_MESSAGE);
79
+			} catch (Exception e) {
80
+				// Print any exceptions.
81
+				e.printStackTrace();
82
+			}
83
+		} else {
84
+			// If the config file exists...
85
+			this.logger.info("No TNT: Loading config.yml...");
86
+			
87
+			try {
88
+				// ...load it.
89
+				Configuration config = new Configuration(configYML);
90
+				config.load();
91
+				
92
+				// If the file is blank or missing the warning configuration,
93
+				if (config.getString("notnt.warning") == null) {
94
+					// Set it to the default one and save it.
95
+					config.setProperty("notnt.warning", DEFAULT_MESSAGE);
96
+					config.save();
97
+				}
98
+				
99
+				// Set the instance field for the warning.
100
+				setWarning(config.getString("notnt.warning"));
101
+			} catch (Exception e) {
102
+				// Print any exceptions.
103
+				e.printStackTrace();
104
+			}
105
+		}
106
+	}
107
+	
108
+	/**
109
+	 * Sets the warning message instance field for the block listener.
110
+	 * @param s the string to set it to
111
+	 */
112
+	public void setWarning(String s) {
113
+		warning = s;
114
+	}
115
+	
116
+	/**
117
+	 * Returns the warning message as a string for the block listener.
118
+	 * @return the warning message as a string
119
+	 */
120
+	public String getWarning() {
121
+		return warning;
122
+	}
123
+}

+ 58
- 0
src/com/ianonavy/notnt/NoTNTExplosionListener.java View File

@@ -0,0 +1,58 @@
1
+package com.ianonavy.notnt;
2
+
3
+import org.bukkit.Location;
4
+import org.bukkit.entity.Player;
5
+import org.bukkit.entity.TNTPrimed;
6
+import org.bukkit.event.entity.EntityExplodeEvent;
7
+import org.bukkit.event.entity.EntityListener;
8
+
9
+/**
10
+ * Entity explosion listener for the No TNT plugin.
11
+ * @author ianonavy
12
+ * @version 0.2
13
+ */
14
+public class NoTNTExplosionListener extends EntityListener {
15
+	// Instance field
16
+	public static NoTNT  plugin;
17
+		
18
+	/**
19
+	 * Constructor for the No TNT block listener. Retrieves the instance of the main class.
20
+	 * @param instance
21
+	 */
22
+	public NoTNTExplosionListener(NoTNT instance) {
23
+		plugin = instance;
24
+	}
25
+	
26
+	/**
27
+	 * Event listener for explosions
28
+	 */
29
+	public void onEntityExplosion(EntityExplodeEvent event) {
30
+		if (event.getEntity().getClass() == TNTPrimed.class) {
31
+			event.setCancelled(true);
32
+			
33
+			String warning = plugin.getWarning();
34
+			
35
+			double x = event.getLocation().getBlockX();
36
+			double y = event.getLocation().getBlockX();
37
+			double z = event.getLocation().getBlockX();
38
+			int range = 10;
39
+			 
40
+			String playerList = "";
41
+			for (Player player : event.getLocation().getWorld().getPlayers()) {
42
+				Location playerLoc = player.getLocation();
43
+				if (playerLoc.getX() - x < range ||
44
+					playerLoc.getY() - y < range || 
45
+					playerLoc.getZ() - z < range) {
46
+					playerList = playerList + player.getDisplayName() + " ";
47
+				}
48
+			}
49
+			
50
+			plugin.getServer().broadcastMessage(
51
+				warning.replaceFirst("{x}", x + "")
52
+					.replaceFirst("{y}", y + "")
53
+					.replaceFirst("{z}", z + "")
54
+					.replaceFirst("{playerlist}", playerList)
55
+			);
56
+		}
57
+	}
58
+}

Loading…
Cancel
Save