Browse Source

Initial commit

Samuel Čavoj 3 years ago
commit
176e8b70e6

+ 14 - 0
.gitignore

@@ -0,0 +1,14 @@
+target/
+pom.xml.tag
+pom.xml.releaseBackup
+pom.xml.versionsBackup
+pom.xml.next
+release.properties
+dependency-reduced-pom.xml
+buildNumber.properties
+.mvn/timing.properties
+.mvn/wrapper/maven-wrapper.jar
+.classpath
+.project
+.settings/
+.vscode/

+ 44 - 0
pom.xml

@@ -0,0 +1,44 @@
+<?xml version="1.0" encoding="UTF-8"?>
+
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+  <modelVersion>4.0.0</modelVersion>
+
+  <groupId>dev.sammko.onepunch</groupId>
+  <artifactId>OnePunch</artifactId>
+  <version>0.1</version>
+
+  <name>OnePunch</name>
+
+  <packaging>jar</packaging>
+  <properties>
+    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
+    <maven.compiler.source>1.8</maven.compiler.source>
+    <maven.compiler.target>1.8</maven.compiler.target>
+  </properties>
+  <repositories>
+      <repository>
+          <id>spigot-repo</id>
+          <url>https://hub.spigotmc.org/nexus/content/repositories/snapshots/</url>
+      </repository>
+  </repositories>
+  <dependencies>
+      <dependency>
+            <groupId>org.spigotmc</groupId>
+            <artifactId>spigot-api</artifactId>
+            <version>1.13.2-R0.1-SNAPSHOT</version>
+            <scope>provided</scope>
+      </dependency>
+  </dependencies>
+  <build>
+    <sourceDirectory>${project.basedir}/src/main/java</sourceDirectory>
+    <resources>
+      <resource>
+        <directory>${project.basedir}/src/main/resources</directory>
+        <includes>
+          <include>plugin.yml</include>
+        </includes>
+      </resource>
+    </resources>
+  </build>
+</project>

+ 29 - 0
src/main/java/dev/sammko/onepunch/OnePunch.java

@@ -0,0 +1,29 @@
+package dev.sammko.onepunch;
+
+import org.bukkit.plugin.java.JavaPlugin;
+
+/**
+ * ONE PUNCH!!!!
+ *
+ */
+public class OnePunch extends JavaPlugin {
+
+    public PlayerInfoManager playerInfoManager;
+
+    public OnePunch() {
+        playerInfoManager = new PlayerInfoManager();
+    }
+
+    @Override
+    public void onEnable() {
+        getLogger().info("ONE PUNCH!!!!");
+        playerInfoManager.loadFromConfig(getConfig());
+        getServer().getPluginManager().registerEvents(new PunchListener(this), this);
+    }
+
+    @Override
+    public void onDisable() {
+        playerInfoManager.saveToConfig(getConfig());
+        saveConfig();
+    }
+}

+ 30 - 0
src/main/java/dev/sammko/onepunch/PlayerInfo.java

@@ -0,0 +1,30 @@
+package dev.sammko.onepunch;
+
+import java.util.HashMap;
+
+import org.bukkit.Material;
+import org.bukkit.configuration.MemorySection;
+
+public class PlayerInfo {
+    private int skill;
+
+    public void processPunch(Material target) {
+        this.skill++;
+    }
+
+    public int getSkill() {
+        return this.skill;
+    }
+
+    public static PlayerInfo deserialize(MemorySection data) {
+        PlayerInfo pi = new PlayerInfo();
+        pi.skill = data.getInt("skill");
+        return pi;
+    }
+
+    public HashMap<String, Object> serialize() {
+        HashMap<String, Object> m = new HashMap<String, Object>();
+        m.put("skill", this.skill);
+        return m;
+    }
+}

+ 47 - 0
src/main/java/dev/sammko/onepunch/PlayerInfoManager.java

@@ -0,0 +1,47 @@
+package dev.sammko.onepunch;
+
+import java.util.Map;
+import java.util.UUID;
+import java.util.HashMap;
+
+import org.bukkit.entity.Player;
+import org.bukkit.configuration.MemorySection;
+import org.bukkit.configuration.file.FileConfiguration;
+
+public class PlayerInfoManager {
+    private HashMap<UUID, PlayerInfo> infomap;
+    private boolean loaded = false;
+
+    public PlayerInfoManager() {
+        this.infomap = new HashMap<UUID, PlayerInfo>();
+    }
+
+    public PlayerInfo getPlayerInfo(Player p) {
+        PlayerInfo pi = infomap.get(p.getUniqueId());
+        if (pi == null) {
+            pi = new PlayerInfo();
+            infomap.put(p.getUniqueId(), pi);
+        }
+        return pi;
+    }
+
+    public void loadFromConfig(FileConfiguration conf) {
+        assert !loaded;
+        MemorySection root = (MemorySection) conf.get("playerInfo");
+        for (Map.Entry<String, Object> e : root.getValues(false).entrySet()) {
+            UUID u = UUID.fromString(e.getKey());
+            PlayerInfo pi = PlayerInfo.deserialize((MemorySection) e.getValue());
+            infomap.put(u, pi);
+        }
+        loaded = true;
+    }
+
+    public void saveToConfig(FileConfiguration conf) {
+        assert loaded;
+        HashMap<String, HashMap<String, Object>> map =
+            new HashMap<String, HashMap<String, Object>>();
+        for (Map.Entry<UUID, PlayerInfo> e : infomap.entrySet())
+            map.put(e.getKey().toString(), e.getValue().serialize());
+        conf.set("playerInfo", map);
+    }
+}

+ 37 - 0
src/main/java/dev/sammko/onepunch/PunchListener.java

@@ -0,0 +1,37 @@
+package dev.sammko.onepunch;
+
+import java.util.HashMap;
+
+import org.bukkit.Location;
+import org.bukkit.entity.Player;
+import org.bukkit.event.EventHandler;
+import org.bukkit.event.Listener;
+import org.bukkit.event.block.Action;
+import org.bukkit.event.player.PlayerInteractEvent;
+
+public class PunchListener implements Listener {
+
+    private HashMap<Player, Location> lastHit;
+    private OnePunch onePunch;
+
+    public PunchListener(OnePunch op) {
+        this.lastHit = new HashMap<Player, Location>();
+        this.onePunch = op;
+    }
+
+    @EventHandler
+    public void onPlayerInteract(PlayerInteractEvent ev) {
+        if (!ev.hasItem() && ev.getAction() == Action.LEFT_CLICK_BLOCK) {
+            Player player = ev.getPlayer();
+            Location thisloc = ev.getClickedBlock().getLocation();
+            Location lastloc = lastHit.get(player);
+            if (lastloc != null && thisloc.equals(lastloc)) {
+                PlayerInfo playerInfo = onePunch.playerInfoManager.getPlayerInfo(player);
+                playerInfo.processPunch(ev.getClickedBlock().getType());
+                player.sendMessage("Your skill is now: " + playerInfo.getSkill());
+            } else {
+                lastHit.put(player, thisloc);
+            }
+        }
+    }
+}

+ 3 - 0
src/main/resources/plugin.yml

@@ -0,0 +1,3 @@
+main: dev.sammko.onepunch.OnePunch
+name: OnePunch
+version: 0.1