diff --git a/Pvptoggle-1.4-FINAL.jar b/Pvptoggle-1.4-FINAL.jar new file mode 100644 index 0000000..7bd0ed1 Binary files /dev/null and b/Pvptoggle-1.4-FINAL.jar differ diff --git a/pvptogglepluginworkspace/.idea/.gitignore b/pvptogglepluginworkspace/.idea/.gitignore new file mode 100644 index 0000000..26d3352 --- /dev/null +++ b/pvptogglepluginworkspace/.idea/.gitignore @@ -0,0 +1,3 @@ +# Default ignored files +/shelf/ +/workspace.xml diff --git a/pvptogglepluginworkspace/.idea/compiler.xml b/pvptogglepluginworkspace/.idea/compiler.xml new file mode 100644 index 0000000..435944c --- /dev/null +++ b/pvptogglepluginworkspace/.idea/compiler.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/pvptogglepluginworkspace/.idea/encodings.xml b/pvptogglepluginworkspace/.idea/encodings.xml new file mode 100644 index 0000000..aa00ffa --- /dev/null +++ b/pvptogglepluginworkspace/.idea/encodings.xml @@ -0,0 +1,7 @@ + + + + + + + \ No newline at end of file diff --git a/pvptogglepluginworkspace/.idea/jarRepositories.xml b/pvptogglepluginworkspace/.idea/jarRepositories.xml new file mode 100644 index 0000000..861936a --- /dev/null +++ b/pvptogglepluginworkspace/.idea/jarRepositories.xml @@ -0,0 +1,30 @@ + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/pvptogglepluginworkspace/.idea/misc.xml b/pvptogglepluginworkspace/.idea/misc.xml new file mode 100644 index 0000000..40f5889 --- /dev/null +++ b/pvptogglepluginworkspace/.idea/misc.xml @@ -0,0 +1,19 @@ + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/pvptogglepluginworkspace/.idea/modules.xml b/pvptogglepluginworkspace/.idea/modules.xml new file mode 100644 index 0000000..01daa2d --- /dev/null +++ b/pvptogglepluginworkspace/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/pvptogglepluginworkspace/Pvptoggle.iml b/pvptogglepluginworkspace/Pvptoggle.iml new file mode 100644 index 0000000..fa63d4b --- /dev/null +++ b/pvptogglepluginworkspace/Pvptoggle.iml @@ -0,0 +1,12 @@ + + + + + + + SPIGOT + + + + + \ No newline at end of file diff --git a/pvptogglepluginworkspace/pom.xml b/pvptogglepluginworkspace/pom.xml new file mode 100644 index 0000000..4a47c27 --- /dev/null +++ b/pvptogglepluginworkspace/pom.xml @@ -0,0 +1,80 @@ + + + 4.0.0 + + benkralex + Pvptoggle + 1.4-FINAL + jar + + Pvptoggle + + + 1.8 + UTF-8 + + + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.8.1 + + ${java.version} + ${java.version} + + + + org.apache.maven.plugins + maven-shade-plugin + 3.2.4 + + + package + + shade + + + false + + + + + + + + src/main/resources + true + + + + + + + spigotmc-repo + https://hub.spigotmc.org/nexus/content/repositories/snapshots/ + + + sonatype + https://oss.sonatype.org/content/groups/public/ + + + + + + org.spigotmc + spigot-api + 1.20.1-R0.1-SNAPSHOT + provided + + + dev.jorel + commandapi-bukkit-core + 9.0.3 + provided + + + diff --git a/pvptogglepluginworkspace/src/main/java/benkralex/pvptoggle/Config.java b/pvptogglepluginworkspace/src/main/java/benkralex/pvptoggle/Config.java new file mode 100644 index 0000000..d7bff1a --- /dev/null +++ b/pvptogglepluginworkspace/src/main/java/benkralex/pvptoggle/Config.java @@ -0,0 +1,20 @@ +package benkralex.pvptoggle; + +import org.bukkit.configuration.file.FileConfiguration; + +import java.io.File; +import java.time.Instant; + +public class Config { + public static void createConfig() { + FileConfiguration config = Pvptoggle.pvptoggle.getConfig(); + config.options().copyDefaults(); + config.addDefault("pvp-time-hit-back", 60); + Pvptoggle.pvptoggle.saveConfig(); + } + + public static int getpvptime() { + FileConfiguration config = Pvptoggle.pvptoggle.getConfig(); + return config.getInt("pvp-time-hit-back"); + } +} diff --git a/pvptogglepluginworkspace/src/main/java/benkralex/pvptoggle/PvpCommand.java b/pvptogglepluginworkspace/src/main/java/benkralex/pvptoggle/PvpCommand.java new file mode 100644 index 0000000..4266553 --- /dev/null +++ b/pvptogglepluginworkspace/src/main/java/benkralex/pvptoggle/PvpCommand.java @@ -0,0 +1,35 @@ +package benkralex.pvptoggle; + +import dev.jorel.commandapi.CommandAPI; +import dev.jorel.commandapi.CommandAPICommand; +import dev.jorel.commandapi.executors.CommandArguments; +import org.bukkit.NamespacedKey; +import org.bukkit.entity.Player; +import org.bukkit.persistence.PersistentDataContainer; +import org.bukkit.persistence.PersistentDataType; + +public class PvpCommand { + public static void createPvpCommand() { + //Create PVP-Command with Command-API + new CommandAPICommand("pvp"). + withSubcommand(new CommandAPICommand("toggle"). + executesPlayer((sender, args)->{ + pvpToggle(sender, args); + }). + withPermission("pvp.toggle"). + withUsage("/pvp toggle"). + withHelp("PvP damage für sich an/auschalten.", "Du kannst damit an/ausschalten, ob du geschlagen werden kannst.")). + register(); + } + + public static void pvpToggle(Player sender, CommandArguments args) { + PersistentDataContainer pdc = sender.getPersistentDataContainer(); + if (pdc.has(new NamespacedKey(Pvptoggle.pvptoggle, "pvptoggle"), PersistentDataType.BOOLEAN)) { + pdc.set(new NamespacedKey(Pvptoggle.pvptoggle, "pvptoggle"), PersistentDataType.BOOLEAN, + !pdc.get(new NamespacedKey(Pvptoggle.pvptoggle, "pvptoggle"), PersistentDataType.BOOLEAN)); + sender.sendMessage("Dein PvP-Schutz ist jetzt " + (pdc.get(new NamespacedKey(Pvptoggle.pvptoggle, "pvptoggle"), PersistentDataType.BOOLEAN)?"an":"aus")); + } else { + pdc.set(new NamespacedKey(Pvptoggle.pvptoggle, "pvptoggle"), PersistentDataType.BOOLEAN, true); + } + } +} diff --git a/pvptogglepluginworkspace/src/main/java/benkralex/pvptoggle/PvpEvent.java b/pvptogglepluginworkspace/src/main/java/benkralex/pvptoggle/PvpEvent.java new file mode 100644 index 0000000..c915bf1 --- /dev/null +++ b/pvptogglepluginworkspace/src/main/java/benkralex/pvptoggle/PvpEvent.java @@ -0,0 +1,83 @@ +package benkralex.pvptoggle; + + +import org.bukkit.NamespacedKey; +import org.bukkit.OfflinePlayer; +import org.bukkit.entity.Entity; +import org.bukkit.entity.Player; +import org.bukkit.event.EventHandler; +import org.bukkit.event.Listener; +import org.bukkit.event.entity.EntityDamageByEntityEvent; +import org.bukkit.persistence.PersistentDataAdapterContext; +import org.bukkit.persistence.PersistentDataContainer; +import org.bukkit.persistence.PersistentDataType; +import sun.reflect.generics.tree.ArrayTypeSignature; + +import java.sql.Array; +import java.time.Instant; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Date; +import java.util.List; + +public class PvpEvent implements Listener { + + @EventHandler + public static void pvpListener(EntityDamageByEntityEvent event) { + Entity damager = event.getDamager(); + Entity victim = event.getEntity(); + + if (damager instanceof Player && victim instanceof Player) { + List pvpvictimsList; + List pvpvictimsListdamager; + PersistentDataContainer pdc = victim.getPersistentDataContainer(); + PersistentDataContainer pdcdamager = damager.getPersistentDataContainer(); + PersistentDataContainer[] pvpvictims = pdc.get(new NamespacedKey(Pvptoggle.pvptoggle, "pvpvictims"), PersistentDataType.TAG_CONTAINER_ARRAY); + PersistentDataContainer[] pvpvictimsdamager = pdcdamager.get(new NamespacedKey(Pvptoggle.pvptoggle, "pvpvictims"), PersistentDataType.TAG_CONTAINER_ARRAY); + boolean canpvp = false; + String victimuuid = victim.getUniqueId().toString(); + + if (pvpvictims != null) { + pvpvictimsList = new ArrayList(Arrays.asList(pvpvictims)); + } else { + pvpvictimsList = new ArrayList(); + } + + if (pvpvictimsdamager != null) { + pvpvictimsListdamager = new ArrayList(Arrays.asList(pvpvictimsdamager)); + } else { + pvpvictimsListdamager = new ArrayList(); + } + + for (int i = 0; i= Instant.now().getEpochSecond() - Config.getpvptime()) { + if (pdcfor.get(new NamespacedKey(Pvptoggle.pvptoggle, "uuid"), PersistentDataType.STRING).equals(victimuuid)) { + canpvp = true; + } + } else { + pvpvictimsListdamager.remove(i); + i--; + } + } + + if (pdc.has(new NamespacedKey(Pvptoggle.pvptoggle, "pvptoggle"), PersistentDataType.BOOLEAN) && + pdc.get(new NamespacedKey(Pvptoggle.pvptoggle, "pvptoggle"), PersistentDataType.BOOLEAN) && + !canpvp) { + + event.setCancelled(true); + + } else { + PersistentDataContainer pdcupdated = pdc.getAdapterContext().newPersistentDataContainer(); + String uuid = damager.getUniqueId().toString(); + pdcupdated.set(new NamespacedKey(Pvptoggle.pvptoggle, "uuid"), PersistentDataType.STRING, uuid); + pdcupdated.set(new NamespacedKey(Pvptoggle.pvptoggle, "time"), PersistentDataType.LONG, Instant.now().getEpochSecond()); + pvpvictimsList.add(pdcupdated); + pvpvictims = pvpvictimsList.toArray(new PersistentDataContainer[pvpvictimsList.size()]); + pdc.set(new NamespacedKey(Pvptoggle.pvptoggle, "pvpvictims"), PersistentDataType.TAG_CONTAINER_ARRAY, pvpvictims); + } + } + } + +} +//pdc.getAdapterContext().newPersistentDataContainer() \ No newline at end of file diff --git a/pvptogglepluginworkspace/src/main/java/benkralex/pvptoggle/Pvptoggle.java b/pvptogglepluginworkspace/src/main/java/benkralex/pvptoggle/Pvptoggle.java new file mode 100644 index 0000000..d349af0 --- /dev/null +++ b/pvptogglepluginworkspace/src/main/java/benkralex/pvptoggle/Pvptoggle.java @@ -0,0 +1,26 @@ +package benkralex.pvptoggle; + +import org.bukkit.Bukkit; +import org.bukkit.command.Command; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; +import org.bukkit.plugin.java.JavaPlugin; + +public final class Pvptoggle extends JavaPlugin { + + public static Pvptoggle pvptoggle; + + @Override + public void onEnable() { + // Plugin enable logic + pvptoggle = this; + Bukkit.getPluginManager().registerEvents(new PvpEvent(), this); + PvpCommand.createPvpCommand(); + Config.createConfig(); + } + + @Override + public void onDisable() { + // Plugin shutdown logic + } +} \ No newline at end of file diff --git a/pvptogglepluginworkspace/src/main/resources/plugin.yml b/pvptogglepluginworkspace/src/main/resources/plugin.yml new file mode 100644 index 0000000..b1b10c3 --- /dev/null +++ b/pvptogglepluginworkspace/src/main/resources/plugin.yml @@ -0,0 +1,7 @@ +name: Pvptoggle +version: '${project.version}' +main: benkralex.pvptoggle.Pvptoggle +api-version: 1.20 +permissions: + pvp.toggle: + default: true \ No newline at end of file