diff --git a/.gitignore b/.gitignore index 9e197e4..98aa1ae 100644 --- a/.gitignore +++ b/.gitignore @@ -149,3 +149,4 @@ fabric.properties # Android studio 3.1+ serialized cache file .idea/caches/build_file_checksums.ser +run/ \ No newline at end of file diff --git a/.idea/.name b/.idea/.name new file mode 100644 index 0000000..b571437 --- /dev/null +++ b/.idea/.name @@ -0,0 +1 @@ +the5gi \ No newline at end of file diff --git a/.idea/compiler.xml b/.idea/compiler.xml new file mode 100644 index 0000000..b589d56 --- /dev/null +++ b/.idea/compiler.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/discord.xml b/.idea/discord.xml new file mode 100644 index 0000000..30bab2a --- /dev/null +++ b/.idea/discord.xml @@ -0,0 +1,7 @@ + + + + + \ No newline at end of file diff --git a/.idea/jarRepositories.xml b/.idea/jarRepositories.xml new file mode 100644 index 0000000..0f3ad39 --- /dev/null +++ b/.idea/jarRepositories.xml @@ -0,0 +1,35 @@ + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..16ec47a --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,18 @@ + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/modules/the5gi.main.iml b/.idea/modules/the5gi.main.iml new file mode 100644 index 0000000..fa63d4b --- /dev/null +++ b/.idea/modules/the5gi.main.iml @@ -0,0 +1,12 @@ + + + + + + + SPIGOT + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/build.gradle b/build.gradle new file mode 100644 index 0000000..9723ccc --- /dev/null +++ b/build.gradle @@ -0,0 +1,38 @@ +plugins { + id 'java' + id 'xyz.jpenilla.run-paper' version "1.0.6" +} + +repositories { + mavenLocal() + maven { + url = uri('https://hub.spigotmc.org/nexus/content/repositories/snapshots/') + } + + maven { + url = uri('https://oss.sonatype.org/content/groups/public/') + } + + maven { + url = uri('https://repo.maven.apache.org/maven2/') + } +} + +dependencies { + compileOnly 'org.spigotmc:spigot-api:1.19-R0.1-SNAPSHOT' +} + +group = 'com' +version = '1.0.0' +description = 'SimpleTTT' +java.sourceCompatibility = JavaVersion.VERSION_17 + +tasks.withType(JavaCompile) { + options.encoding = 'UTF-8' +} + +tasks { + runServer { + minecraftVersion("1.19") + } +} diff --git a/gradle/wrapper/gradle-wrapper.jar b/gradle/wrapper/gradle-wrapper.jar new file mode 100644 index 0000000..41d9927 Binary files /dev/null and b/gradle/wrapper/gradle-wrapper.jar differ diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties new file mode 100644 index 0000000..aa991fc --- /dev/null +++ b/gradle/wrapper/gradle-wrapper.properties @@ -0,0 +1,5 @@ +distributionBase=GRADLE_USER_HOME +distributionPath=wrapper/dists +distributionUrl=https\://services.gradle.org/distributions/gradle-7.4.2-bin.zip +zipStoreBase=GRADLE_USER_HOME +zipStorePath=wrapper/dists diff --git a/gradlew b/gradlew new file mode 100644 index 0000000..1b6c787 --- /dev/null +++ b/gradlew @@ -0,0 +1,234 @@ +#!/bin/sh + +# +# Copyright © 2015-2021 the original authors. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +############################################################################## +# +# Gradle start up script for POSIX generated by Gradle. +# +# Important for running: +# +# (1) You need a POSIX-compliant shell to run this script. If your /bin/sh is +# noncompliant, but you have some other compliant shell such as ksh or +# bash, then to run this script, type that shell name before the whole +# command line, like: +# +# ksh Gradle +# +# Busybox and similar reduced shells will NOT work, because this script +# requires all of these POSIX shell features: +# * functions; +# * expansions «$var», «${var}», «${var:-default}», «${var+SET}», +# «${var#prefix}», «${var%suffix}», and «$( cmd )»; +# * compound commands having a testable exit status, especially «case»; +# * various built-in commands including «command», «set», and «ulimit». +# +# Important for patching: +# +# (2) This script targets any POSIX shell, so it avoids extensions provided +# by Bash, Ksh, etc; in particular arrays are avoided. +# +# The "traditional" practice of packing multiple parameters into a +# space-separated string is a well documented source of bugs and security +# problems, so this is (mostly) avoided, by progressively accumulating +# options in "$@", and eventually passing that to Java. +# +# Where the inherited environment variables (DEFAULT_JVM_OPTS, JAVA_OPTS, +# and GRADLE_OPTS) rely on word-splitting, this is performed explicitly; +# see the in-line comments for details. +# +# There are tweaks for specific operating systems such as AIX, CygWin, +# Darwin, MinGW, and NonStop. +# +# (3) This script is generated from the Groovy template +# https://github.com/gradle/gradle/blob/master/subprojects/plugins/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt +# within the Gradle project. +# +# You can find Gradle at https://github.com/gradle/gradle/. +# +############################################################################## + +# Attempt to set APP_HOME + +# Resolve links: $0 may be a link +app_path=$0 + +# Need this for daisy-chained symlinks. +while + APP_HOME=${app_path%"${app_path##*/}"} # leaves a trailing /; empty if no leading path + [ -h "$app_path" ] +do + ls=$( ls -ld "$app_path" ) + link=${ls#*' -> '} + case $link in #( + /*) app_path=$link ;; #( + *) app_path=$APP_HOME$link ;; + esac +done + +APP_HOME=$( cd "${APP_HOME:-./}" && pwd -P ) || exit + +APP_NAME="Gradle" +APP_BASE_NAME=${0##*/} + +# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"' + +# Use the maximum available, or set MAX_FD != -1 to use that value. +MAX_FD=maximum + +warn () { + echo "$*" +} >&2 + +die () { + echo + echo "$*" + echo + exit 1 +} >&2 + +# OS specific support (must be 'true' or 'false'). +cygwin=false +msys=false +darwin=false +nonstop=false +case "$( uname )" in #( + CYGWIN* ) cygwin=true ;; #( + Darwin* ) darwin=true ;; #( + MSYS* | MINGW* ) msys=true ;; #( + NONSTOP* ) nonstop=true ;; +esac + +CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar + + +# Determine the Java command to use to start the JVM. +if [ -n "$JAVA_HOME" ] ; then + if [ -x "$JAVA_HOME/jre/sh/java" ] ; then + # IBM's JDK on AIX uses strange locations for the executables + JAVACMD=$JAVA_HOME/jre/sh/java + else + JAVACMD=$JAVA_HOME/bin/java + fi + if [ ! -x "$JAVACMD" ] ; then + die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." + fi +else + JAVACMD=java + which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." +fi + +# Increase the maximum file descriptors if we can. +if ! "$cygwin" && ! "$darwin" && ! "$nonstop" ; then + case $MAX_FD in #( + max*) + MAX_FD=$( ulimit -H -n ) || + warn "Could not query maximum file descriptor limit" + esac + case $MAX_FD in #( + '' | soft) :;; #( + *) + ulimit -n "$MAX_FD" || + warn "Could not set maximum file descriptor limit to $MAX_FD" + esac +fi + +# Collect all arguments for the java command, stacking in reverse order: +# * args from the command line +# * the main class name +# * -classpath +# * -D...appname settings +# * --module-path (only if needed) +# * DEFAULT_JVM_OPTS, JAVA_OPTS, and GRADLE_OPTS environment variables. + +# For Cygwin or MSYS, switch paths to Windows format before running java +if "$cygwin" || "$msys" ; then + APP_HOME=$( cygpath --path --mixed "$APP_HOME" ) + CLASSPATH=$( cygpath --path --mixed "$CLASSPATH" ) + + JAVACMD=$( cygpath --unix "$JAVACMD" ) + + # Now convert the arguments - kludge to limit ourselves to /bin/sh + for arg do + if + case $arg in #( + -*) false ;; # don't mess with options #( + /?*) t=${arg#/} t=/${t%%/*} # looks like a POSIX filepath + [ -e "$t" ] ;; #( + *) false ;; + esac + then + arg=$( cygpath --path --ignore --mixed "$arg" ) + fi + # Roll the args list around exactly as many times as the number of + # args, so each arg winds up back in the position where it started, but + # possibly modified. + # + # NB: a `for` loop captures its iteration list before it begins, so + # changing the positional parameters here affects neither the number of + # iterations, nor the values presented in `arg`. + shift # remove old arg + set -- "$@" "$arg" # push replacement arg + done +fi + +# Collect all arguments for the java command; +# * $DEFAULT_JVM_OPTS, $JAVA_OPTS, and $GRADLE_OPTS can contain fragments of +# shell script including quotes and variable substitutions, so put them in +# double quotes to make sure that they get re-expanded; and +# * put everything else in single quotes, so that it's not re-expanded. + +set -- \ + "-Dorg.gradle.appname=$APP_BASE_NAME" \ + -classpath "$CLASSPATH" \ + org.gradle.wrapper.GradleWrapperMain \ + "$@" + +# Use "xargs" to parse quoted args. +# +# With -n1 it outputs one arg per line, with the quotes and backslashes removed. +# +# In Bash we could simply go: +# +# readarray ARGS < <( xargs -n1 <<<"$var" ) && +# set -- "${ARGS[@]}" "$@" +# +# but POSIX shell has neither arrays nor command substitution, so instead we +# post-process each arg (as a line of input to sed) to backslash-escape any +# character that might be a shell metacharacter, then use eval to reverse +# that process (while maintaining the separation between arguments), and wrap +# the whole thing up as a single "set" statement. +# +# This will of course break if any of these variables contains a newline or +# an unmatched quote. +# + +eval "set -- $( + printf '%s\n' "$DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS" | + xargs -n1 | + sed ' s~[^-[:alnum:]+,./:=@_]~\\&~g; ' | + tr '\n' ' ' + )" '"$@"' + +exec "$JAVACMD" "$@" diff --git a/gradlew.bat b/gradlew.bat new file mode 100644 index 0000000..107acd3 --- /dev/null +++ b/gradlew.bat @@ -0,0 +1,89 @@ +@rem +@rem Copyright 2015 the original author or authors. +@rem +@rem Licensed under the Apache License, Version 2.0 (the "License"); +@rem you may not use this file except in compliance with the License. +@rem You may obtain a copy of the License at +@rem +@rem https://www.apache.org/licenses/LICENSE-2.0 +@rem +@rem Unless required by applicable law or agreed to in writing, software +@rem distributed under the License is distributed on an "AS IS" BASIS, +@rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +@rem See the License for the specific language governing permissions and +@rem limitations under the License. +@rem + +@if "%DEBUG%" == "" @echo off +@rem ########################################################################## +@rem +@rem Gradle startup script for Windows +@rem +@rem ########################################################################## + +@rem Set local scope for the variables with windows NT shell +if "%OS%"=="Windows_NT" setlocal + +set DIRNAME=%~dp0 +if "%DIRNAME%" == "" set DIRNAME=. +set APP_BASE_NAME=%~n0 +set APP_HOME=%DIRNAME% + +@rem Resolve any "." and ".." in APP_HOME to make it shorter. +for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi + +@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m" + +@rem Find java.exe +if defined JAVA_HOME goto findJavaFromJavaHome + +set JAVA_EXE=java.exe +%JAVA_EXE% -version >NUL 2>&1 +if "%ERRORLEVEL%" == "0" goto execute + +echo. +echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. +echo. +echo Please set the JAVA_HOME variable in your environment to match the +echo location of your Java installation. + +goto fail + +:findJavaFromJavaHome +set JAVA_HOME=%JAVA_HOME:"=% +set JAVA_EXE=%JAVA_HOME%/bin/java.exe + +if exist "%JAVA_EXE%" goto execute + +echo. +echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% +echo. +echo Please set the JAVA_HOME variable in your environment to match the +echo location of your Java installation. + +goto fail + +:execute +@rem Setup the command line + +set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar + + +@rem Execute Gradle +"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %* + +:end +@rem End local scope for the variables with windows NT shell +if "%ERRORLEVEL%"=="0" goto mainEnd + +:fail +rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of +rem the _cmd.exe /c_ return code! +if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 +exit /b 1 + +:mainEnd +if "%OS%"=="Windows_NT" endlocal + +:omega diff --git a/settings.gradle b/settings.gradle new file mode 100644 index 0000000..ed4e0c0 --- /dev/null +++ b/settings.gradle @@ -0,0 +1 @@ +rootProject.name = 'the5gi' \ No newline at end of file diff --git a/src/main/java/com/the5gi/SimpleTTTCore.java b/src/main/java/com/the5gi/SimpleTTTCore.java new file mode 100644 index 0000000..7b89aa7 --- /dev/null +++ b/src/main/java/com/the5gi/SimpleTTTCore.java @@ -0,0 +1,48 @@ +package com.the5gi; + +import com.the5gi.commands.StartGameCommand; +import com.the5gi.events.GameStartTTTEvent; +import com.the5gi.events.PreStartTTTEvent; +import com.the5gi.listeners.GameStartTTTListener; +import com.the5gi.listeners.GamesOnTTTListener; +import com.the5gi.listeners.PlayerInteractAtEntityListener; +import com.the5gi.listeners.PreStartTTTListener; +import org.bukkit.Bukkit; +import org.bukkit.entity.Player; +import org.bukkit.plugin.java.JavaPlugin; + +public final class SimpleTTTCore extends JavaPlugin { + + public static int taskId = -1; + + @Override + public void onEnable() { + this.getCommand("startgame").setExecutor(new StartGameCommand(this)); + this.getServer().getPluginManager().registerEvents(new PlayerInteractAtEntityListener(), this); + this.getServer().getPluginManager().registerEvents(new GamesOnTTTListener(), this); + this.getServer().getPluginManager().registerEvents(new GameStartTTTListener(), this); + this.getServer().getPluginManager().registerEvents(new PreStartTTTListener(), this); + //startGame(this); + } + + @Override + public void onDisable() { + // Plugin shutdown logic + } + + public static void startGame(JavaPlugin plugin) { + //taskId = Bukkit.getServer().getScheduler().scheduleSyncRepeatingTask(plugin, () -> { + int playerAmount = plugin.getServer().getOnlinePlayers().size(); + + if (playerAmount >= 2) { + for(Player player : plugin.getServer().getOnlinePlayers()) { + player.sendMessage("yes game should start"); + } + PreStartTTTEvent startTTTEvent = new PreStartTTTEvent(plugin); + plugin.getServer().getPluginManager().callEvent(startTTTEvent); + } + + //}, 20 , 1); + } + +} diff --git a/src/main/java/com/the5gi/commands/StartGameCommand.java b/src/main/java/com/the5gi/commands/StartGameCommand.java new file mode 100644 index 0000000..3ec5991 --- /dev/null +++ b/src/main/java/com/the5gi/commands/StartGameCommand.java @@ -0,0 +1,34 @@ +package com.the5gi.commands; + +import com.the5gi.SimpleTTTCore; +import com.the5gi.util.Role; +import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; +import org.bukkit.plugin.java.JavaPlugin; + +import java.util.Random; + +public class StartGameCommand implements CommandExecutor { + private JavaPlugin plugin; + + public StartGameCommand(JavaPlugin plugin) { + this.plugin = plugin; + } + + @Override + public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { + if (sender instanceof Player) { + Player player = (Player) sender; + if (player.getServer().getOnlinePlayers().size() >= 2) { + player.sendMessage("Game Started"); + //player.sendMessage(getRandomRole().toString()); + SimpleTTTCore.startGame(plugin); + } else { + player.sendMessage("Not enough players to start game!"); + } + } + return true; + } +} diff --git a/src/main/java/com/the5gi/events/GameStartTTTEvent.java b/src/main/java/com/the5gi/events/GameStartTTTEvent.java new file mode 100644 index 0000000..92ded63 --- /dev/null +++ b/src/main/java/com/the5gi/events/GameStartTTTEvent.java @@ -0,0 +1,121 @@ +package com.the5gi.events; + +import com.the5gi.util.Role; +import org.bukkit.entity.Player; +import org.bukkit.event.Event; +import org.bukkit.event.HandlerList; +import org.bukkit.plugin.java.JavaPlugin; + +import java.util.*; + +public class GameStartTTTEvent extends Event { + + private static final HandlerList HANDLER_LIST = new HandlerList(); + + private static JavaPlugin plugin; + + private static int[] roleAmountScheme; + + private static Map playerRoles = new HashMap<>(); + + //private static List playerRoles = new ArrayList<>(); + + //get + + private static List participatingPlayers = new ArrayList<>(); + + + + public GameStartTTTEvent(JavaPlugin plugin, int[] roleAmountScheme, List participatingPlayers) { + GameStartTTTEvent.plugin = plugin; + GameStartTTTEvent.roleAmountScheme = roleAmountScheme; + GameStartTTTEvent.participatingPlayers = participatingPlayers; + } + + public JavaPlugin getPlugin() { + return plugin; + } + + + public int[] getRoleAmountScheme() { + return roleAmountScheme; + } + + public Map getPlayerRoles() { + return playerRoles; + } + + public void setPlayerRole(Player player, Role role) { + playerRoles.put(player, role); + } + + public Role getPlayerRole(Player player) { + return playerRoles.get(player); + } + + public boolean playerHasRole(Player player) { + Role playerRole = getPlayerRole(player); + + if (playerRole != null) { + return true; + } + + return false; + } + + public List getParticipatingPlayers() { + return participatingPlayers; + } + + public Role getRandomRole() { + + Role returnRole = null; + + Random rand = new Random(); + int randomInt = rand.nextInt(9); + + switch (randomInt) { + case 0: + returnRole = Role.DETECTIVE; + break; + case 1: + returnRole = Role.SHERIFF; + break; + case 2: + returnRole = Role.SURVIVALISTS; + break; + case 3: + returnRole = Role.MERCENARIES; + break; + case 4: + returnRole = Role.TRAITORS; + break; + case 5: + returnRole = Role.ASSASSINS; + break; + case 6: + returnRole = Role.LONEWOLF; + break; + case 7: + returnRole = Role.JESTER; + break; + case 8: + returnRole = Role.INNOCENT; + break; + } + + return returnRole; + } + + + + + @Override + public HandlerList getHandlers() { + return HANDLER_LIST; + } + + public static HandlerList getHandlerList() { + return HANDLER_LIST; + } +} diff --git a/src/main/java/com/the5gi/events/PreStartTTTEvent.java b/src/main/java/com/the5gi/events/PreStartTTTEvent.java new file mode 100644 index 0000000..d7a5361 --- /dev/null +++ b/src/main/java/com/the5gi/events/PreStartTTTEvent.java @@ -0,0 +1,39 @@ +package com.the5gi.events; + +import org.bukkit.event.Event; +import org.bukkit.event.HandlerList; +import org.bukkit.plugin.java.JavaPlugin; + +public class PreStartTTTEvent extends Event { + private static final HandlerList HANDLER_LIST = new HandlerList(); + + private static JavaPlugin plugin; + + private boolean cancelled = false; + + + public PreStartTTTEvent(JavaPlugin server) { + plugin = server; + } + + public JavaPlugin getPlugin() { + return plugin; + } + + public boolean isCancelled() { + return cancelled; + } + + public void setCancelled(boolean cancel) { + cancelled = cancel; + } + + @Override + public HandlerList getHandlers() { + return HANDLER_LIST; + } + + public static HandlerList getHandlerList() { + return HANDLER_LIST; + } +} diff --git a/src/main/java/com/the5gi/events/TTTGamesOnEvent.java b/src/main/java/com/the5gi/events/TTTGamesOnEvent.java new file mode 100644 index 0000000..4e9669c --- /dev/null +++ b/src/main/java/com/the5gi/events/TTTGamesOnEvent.java @@ -0,0 +1,60 @@ +package com.the5gi.events; + +import com.the5gi.util.Role; +import org.bukkit.entity.Player; +import org.bukkit.event.Event; +import org.bukkit.event.HandlerList; +import org.bukkit.plugin.java.JavaPlugin; + +import java.util.ArrayList; +import java.util.List; +import java.util.Map; + +public class TTTGamesOnEvent extends Event { + + private static final HandlerList HANDLER_LIST = new HandlerList(); + + private static JavaPlugin plugin; + + private List participatingPlayers = new ArrayList<>(); + private Map playerRoles; + private int[] roleAmounts = {0,0,0,0,0,0,0,0}; + + + public TTTGamesOnEvent(JavaPlugin plugin, List participatingPlayers, Map playerRoles, int[] roleAmounts) { + this.plugin = plugin; + this.participatingPlayers = participatingPlayers; + this.playerRoles = playerRoles; + this.roleAmounts = roleAmounts; + + } + + public int[] getRoleAmounts() { + return roleAmounts; + } + + public JavaPlugin getPlugin() { + return plugin; + } + + public List getPlayers() { + return participatingPlayers; + } + + public Role getPlayerRole(Player player) { + return playerRoles.get(player); + } + + public Role getPlayerRoleByInt(int playerInt) { + return playerRoles.get(playerInt); + } + + @Override + public HandlerList getHandlers() { + return HANDLER_LIST; + } + + public static HandlerList getHandlerList() { + return HANDLER_LIST; + } +} diff --git a/src/main/java/com/the5gi/listeners/GameStartTTTListener.java b/src/main/java/com/the5gi/listeners/GameStartTTTListener.java new file mode 100644 index 0000000..f19a065 --- /dev/null +++ b/src/main/java/com/the5gi/listeners/GameStartTTTListener.java @@ -0,0 +1,48 @@ +package com.the5gi.listeners; + +import com.the5gi.events.GameStartTTTEvent; +import com.the5gi.events.TTTGamesOnEvent; +import com.the5gi.util.Role; +import org.bukkit.Bukkit; +import org.bukkit.entity.Player; +import org.bukkit.event.EventHandler; +import org.bukkit.event.Listener; + +public class GameStartTTTListener implements Listener { + private static int gameTaskId = -1; + private static boolean called = false; + + @EventHandler + public static void gameStartListener(GameStartTTTEvent event) { + int[] desiredAmounts = event.getRoleAmountScheme(); + int[] amounts = desiredAmounts; + + for (Player player : event.getParticipatingPlayers()) { + if (!event.playerHasRole(player)) { + boolean roleIsUnavailable = true; + Role role; + while (roleIsUnavailable) { + role = event.getRandomRole(); + if (amounts[role.getId()] < 1) { + System.out.println(amounts[role.getId()]); + continue; + } + event.setPlayerRole(player, role); + amounts[role.getId()]--; + roleIsUnavailable = false; + } + System.out.println("End while loop"); + } + } + System.out.println("End for loop"); + + gameTaskId = Bukkit.getServer().getScheduler().scheduleSyncRepeatingTask(event.getPlugin(), () -> { + if (!called) { + TTTGamesOnEvent gameOnEvent = new TTTGamesOnEvent(event.getPlugin(), event.getParticipatingPlayers(), event.getPlayerRoles(), desiredAmounts); + event.getPlugin().getServer().getPluginManager().callEvent(gameOnEvent); + event.getPlugin().getServer().getScheduler().cancelTask(gameTaskId); + } + }, 20 , 1); + + } +} diff --git a/src/main/java/com/the5gi/listeners/GamesOnTTTListener.java b/src/main/java/com/the5gi/listeners/GamesOnTTTListener.java new file mode 100644 index 0000000..36979c2 --- /dev/null +++ b/src/main/java/com/the5gi/listeners/GamesOnTTTListener.java @@ -0,0 +1,77 @@ +package com.the5gi.listeners; + +import com.the5gi.SimpleTTTCore; +import com.the5gi.events.TTTGamesOnEvent; +import com.the5gi.util.*; +import net.md_5.bungee.api.ChatMessageType; +import net.md_5.bungee.api.chat.TextComponent; +import org.bukkit.Bukkit; +import org.bukkit.ChatColor; +import org.bukkit.boss.BarColor; +import org.bukkit.boss.BarStyle; +import org.bukkit.boss.BossBar; +import org.bukkit.entity.Player; +import org.bukkit.event.EventHandler; +import org.bukkit.event.Listener; + +public class GamesOnTTTListener implements Listener { + + private static int gamesOnTaskId = -1; + private static int timeLeft = 300; + + public static int playersLeft = -1; + + private static boolean doRush = true; + + @EventHandler + public static void gamesOnEvent(TTTGamesOnEvent event) { + playersLeft = event.getPlayers().size(); + for (Player player : event.getPlayers()) { + System.out.println(event.getPlayerRole(player)); + ChatColor roleColor = RoleColorHelper.getChatColorOfRole(event.getPlayerRole(player)); + player.sendTitle("Your role is: " + roleColor + event.getPlayerRole(player).toString(), "", 20, 80, 20); + player.spigot().sendMessage(ChatMessageType.ACTION_BAR, new TextComponent(roleColor + RoleDescriptionHelper.getRoleDescription(event.getPlayerRole(player)))); + } + + BossBar bossBar = Bukkit.createBossBar("Time Left: -1s", BarColor.YELLOW, BarStyle.SOLID); + for (Player player : event.getPlayers()) { + bossBar.addPlayer(player); + } + gamesOnTaskId = Bukkit.getServer().getScheduler().scheduleSyncRepeatingTask(event.getPlugin(), () -> { + if (timeLeft == -1) { + bossBar.setTitle("Time Left: " + "Overtime"); + bossBar.setColor(BarColor.RED); + } else if (timeLeft != 0) { + bossBar.setTitle("Time Left: " + timeLeft + "s"); + + if (timeLeft <= 10) { + bossBar.setColor(BarColor.RED); + } else if (timeLeft > 30) { + bossBar.setColor(BarColor.GREEN); + } else if (timeLeft > 10 && timeLeft < 30) { + bossBar.setColor(BarColor.YELLOW); + } else { + bossBar.setColor(BarColor.WHITE); + } + timeLeft--; + } else if (timeLeft == 0) { + //DONE Make this not fail with null value after time is up + if (GameHelper.getGameDecision(event.getRoleAmounts(), playersLeft, doRush).equals(GameDecision.RUSH)) { + doRush = false; + timeLeft = 120; + } else if (GameHelper.getGameDecision(event.getRoleAmounts(), playersLeft, doRush).equals(GameDecision.OVERTIME)) { + timeLeft = -1; + } else if (GameHelper.getGameDecision(event.getRoleAmounts(), playersLeft, doRush).equals(GameDecision.END)) { + bossBar.removeAll(); + for (Player player : event.getPlugin().getServer().getOnlinePlayers()) { + player.sendTitle(RoleWinnerHelper.getTitle(GameHelper.getWinner()), "", 10, 60, 10); + } + SimpleTTTCore.startGame(event.getPlugin()); + event.getPlugin().getServer().getScheduler().cancelTask(gamesOnTaskId); + } + } + }, 20 , 20); + + + } +} diff --git a/src/main/java/com/the5gi/listeners/PlayerInteractAtEntityListener.java b/src/main/java/com/the5gi/listeners/PlayerInteractAtEntityListener.java new file mode 100644 index 0000000..8221b66 --- /dev/null +++ b/src/main/java/com/the5gi/listeners/PlayerInteractAtEntityListener.java @@ -0,0 +1,17 @@ +package com.the5gi.listeners; + +import org.bukkit.event.EventHandler; +import org.bukkit.event.Listener; +import org.bukkit.event.player.PlayerInteractAtEntityEvent; +import org.bukkit.inventory.EquipmentSlot; + +public class PlayerInteractAtEntityListener implements Listener { + @EventHandler + public static void onPlayerDeath(PlayerInteractAtEntityEvent event) { + if (event.getHand() == EquipmentSlot.OFF_HAND) { + return; // off hand packet, ignore. + } + + GamesOnTTTListener.playersLeft--; + } +} diff --git a/src/main/java/com/the5gi/listeners/PreStartTTTListener.java b/src/main/java/com/the5gi/listeners/PreStartTTTListener.java new file mode 100644 index 0000000..54ba91d --- /dev/null +++ b/src/main/java/com/the5gi/listeners/PreStartTTTListener.java @@ -0,0 +1,58 @@ +package com.the5gi.listeners; + +import com.the5gi.SimpleTTTCore; +import com.the5gi.events.GameStartTTTEvent; +import com.the5gi.events.PreStartTTTEvent; +import com.the5gi.util.RoleAmountHelper; +import org.bukkit.Bukkit; +import org.bukkit.boss.BarColor; +import org.bukkit.boss.BarStyle; +import org.bukkit.boss.BossBar; +import org.bukkit.entity.Player; +import org.bukkit.event.EventHandler; +import org.bukkit.event.Listener; +import org.bukkit.plugin.Plugin; + +import java.util.ArrayList; +import java.util.List; + +public class PreStartTTTListener implements Listener { + private Plugin plugin; + private static int timeLeft = 30; + private static BossBar bossBar; + public static int taskId = -1; + + @EventHandler + public static void onStart(PreStartTTTEvent event) { + event.getPlugin().getServer().getScheduler().cancelTask(SimpleTTTCore.taskId); + bossBar = Bukkit.createBossBar("Starting in: null", BarColor.YELLOW, BarStyle.SOLID); + taskId = Bukkit.getServer().getScheduler().scheduleSyncRepeatingTask(event.getPlugin(), () -> { + if (event.getPlugin().getServer().getOnlinePlayers().size() >= 2) { + if (timeLeft == 0) { + bossBar.setTitle("Starting Game..."); + bossBar.setColor(BarColor.YELLOW); + bossBar.removeAll(); + List players = new ArrayList<>(); + for (Player player : event.getPlugin().getServer().getOnlinePlayers()) { + players.add(player); + } + GameStartTTTEvent gameStartTTTEvent = new GameStartTTTEvent(event.getPlugin(), RoleAmountHelper.getAmountOfRoles(event.getPlugin().getServer().getOnlinePlayers().size()), players); + event.getPlugin().getServer().getPluginManager().callEvent(gameStartTTTEvent); + event.getPlugin().getServer().getScheduler().cancelTask(taskId); + } else if (timeLeft != 0) { + bossBar.setTitle("Starting in: " + timeLeft + "s"); + bossBar.setColor(BarColor.GREEN); + timeLeft--; + } + } else { + bossBar.setTitle("Not Enough Players!"); + bossBar.setColor(BarColor.RED); + timeLeft = 30; + } + }, 20 , 1); + + } + + + +} diff --git a/src/main/java/com/the5gi/util/GameDecision.java b/src/main/java/com/the5gi/util/GameDecision.java new file mode 100644 index 0000000..08cbb8e --- /dev/null +++ b/src/main/java/com/the5gi/util/GameDecision.java @@ -0,0 +1,7 @@ +package com.the5gi.util; + +public enum GameDecision { + OVERTIME, + RUSH, + END +} diff --git a/src/main/java/com/the5gi/util/GameHelper.java b/src/main/java/com/the5gi/util/GameHelper.java new file mode 100644 index 0000000..72d2762 --- /dev/null +++ b/src/main/java/com/the5gi/util/GameHelper.java @@ -0,0 +1,59 @@ +package com.the5gi.util; + +public class GameHelper { + + private static Role Winner = null; + + //Format: Detectives:Sheriffs:Survivalists:Mercenaries:Traitors:Assassins:LoneWolf:Jester + public static GameDecision getGameDecision(int[] PlayerRolesLeft, int PlayersLeft, boolean doRush) { + int InnocentRoles = PlayersLeft - (PlayerRolesLeft[4] + PlayerRolesLeft[5] + PlayerRolesLeft[6] + PlayerRolesLeft[7]); + int NeutralRoles = PlayerRolesLeft[6] + PlayerRolesLeft[7]; + int TraitorRoles = PlayerRolesLeft[4] + PlayerRolesLeft[5]; + + + if (NeutralRoles == 0 && TraitorRoles > 0) { + + if (InnocentRoles > TraitorRoles && doRush) { + Winner = null; + return GameDecision.RUSH; + } + + if (TraitorRoles > InnocentRoles) { + Winner = Role.INNOCENT; + return GameDecision.END; + } + + if (TraitorRoles == InnocentRoles) { + Winner = null; + return GameDecision.OVERTIME; + } + } else if (NeutralRoles > 0 && TraitorRoles == 0) { + if (NeutralRoles == InnocentRoles) { + Winner = null; + return GameDecision.OVERTIME; + } + + if (NeutralRoles > InnocentRoles) { + Winner = Role.INNOCENT; + return GameDecision.END; + } + + if (InnocentRoles > NeutralRoles && doRush) { + Winner = null; + return GameDecision.RUSH; + } + + if (InnocentRoles > NeutralRoles) { + Winner = null; + return GameDecision.OVERTIME; + } + + } + return null; + } + + public static Role getWinner() { + return Winner; + } + +} diff --git a/src/main/java/com/the5gi/util/Role.java b/src/main/java/com/the5gi/util/Role.java new file mode 100644 index 0000000..f107944 --- /dev/null +++ b/src/main/java/com/the5gi/util/Role.java @@ -0,0 +1,27 @@ +package com.the5gi.util; + +public enum Role { + DETECTIVE(0), + SHERIFF(1), + SURVIVALISTS(2), + MERCENARIES(3), + TRAITORS(4), + ASSASSINS(5), + LONEWOLF(6), + JESTER(7), + INNOCENT(8); + + private int id; + + Role(int id) { + this.id = id; + } + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } +} diff --git a/src/main/java/com/the5gi/util/RoleAmountHelper.java b/src/main/java/com/the5gi/util/RoleAmountHelper.java new file mode 100644 index 0000000..309cd11 --- /dev/null +++ b/src/main/java/com/the5gi/util/RoleAmountHelper.java @@ -0,0 +1,134 @@ +package com.the5gi.util; + +import java.util.Random; + +public class RoleAmountHelper { + + + public static int[] getAmountOfRoles(int PlayerAmount) { + //Format: Detectives:Sheriffs:Survivalists:Mercenaries:Traitors:Assassins:LoneWolf:Jester + // 0, 1, 2, 3, 4, 5, 6, 7, 8 + int[] roleAmount = {0, 0, 0, 0, 0, 0, 0, 0, 0}; + + + + // Detectives + if (PlayerAmount / 10 > 0) { + roleAmount[0] = PlayerAmount / 10; + } else { + roleAmount[0] = 0; + } + + + + // Sheriffs + if (roleAmount[0] == 1) { + Random rand = new Random(); + int random_integer1 = rand.nextInt(4) + 1; + + if (random_integer1 == 3) { + roleAmount[1] = 1; + roleAmount[0] = 0; + } else { + roleAmount[1] = 0; + } + } else if (roleAmount[0] >= 2) { + Random rand = new Random(); + int random_integer1 = rand.nextInt(4) + 1; + + if (random_integer1 == 3) { + roleAmount[1] = (roleAmount[0] / 2); + roleAmount[0] = (roleAmount[0] / 2); + } else { + roleAmount[1] = 0; + } + } else { + roleAmount[1] = 0; + } + + + + // Survivalists + if (roleAmount[0] > 2) { + roleAmount[2] = (roleAmount[0] / 2); + } else if (roleAmount[0] == 1) { + roleAmount[2] = 1; + } else { + roleAmount[2] = 0; + } + + + + // Mercenaries + if (roleAmount[0] > 2) { + roleAmount[3] = (roleAmount[0] / 2); + } else if (roleAmount[0] == 1) { + roleAmount[3] = 1; + } else { + roleAmount[3] = 0; + } + + + + // Traitors + if (roleAmount[0] >= 1) { + roleAmount[4] = (roleAmount[0] * 1); + } else if (roleAmount[0] == 0) { + roleAmount[4] = 2; + } else { + roleAmount[4] = 1; + } + + + + // Assassins + if (roleAmount[4] >= 2) { + roleAmount[5] = (roleAmount[4] / 3); + roleAmount[4] = roleAmount[4] / 2; + } else if (roleAmount[4] == 1) { + roleAmount[5] = 0; + } else { + roleAmount[5] = 0; + } + + + // LoneWold + roleAmount[6] = chooseProbability(roleAmount[0]); + + // Jester + roleAmount[7] = chooseProbability(roleAmount[0]); + + + + roleAmount[8] = (roleAmount[0] + roleAmount[1] + roleAmount[2] + roleAmount[3] + roleAmount[4] + roleAmount[5] + roleAmount[6] + roleAmount[7]); + + /*Innocents = Innocents - (Detectives + Survivalists + Mercenaries + Traitors + Assassins + LoneWolf + Jester); + + + roleAmount + String roleAmountString = Detectives + ":" + Sheriffs + ":" + Survivalists + ":" + Mercenaries + ":" + Traitors + ":" + Assassins + ":" + LoneWolf + ":" + Jester; + return roleAmountString;*/ + return roleAmount; + } + + //3 / 2 + + + private static int chooseProbability(int detectives) { + int probability = 0; + Random rand = new Random(); + int random_integer = rand.nextInt(1); + if (detectives == 1) { + if (random_integer == 0) { + probability = 0; + } else if (random_integer == 1) { + probability = 1; + } else { + probability = 0; + } + } else probability = 0; + return probability; + } + + +} diff --git a/src/main/java/com/the5gi/util/RoleColorHelper.java b/src/main/java/com/the5gi/util/RoleColorHelper.java new file mode 100644 index 0000000..f585ea7 --- /dev/null +++ b/src/main/java/com/the5gi/util/RoleColorHelper.java @@ -0,0 +1,31 @@ +package com.the5gi.util; + +import org.bukkit.ChatColor; + +public class RoleColorHelper { + + public static ChatColor getChatColorOfRole(Role role) { + switch (role) { + case DETECTIVE: + return ChatColor.BLUE; + case SHERIFF: + return ChatColor.DARK_BLUE; + case SURVIVALISTS: + return ChatColor.YELLOW; + case MERCENARIES: + return ChatColor.DARK_GREEN; + case TRAITORS: + return ChatColor.RED; + case ASSASSINS: + return ChatColor.DARK_RED; + case LONEWOLF: + return ChatColor.GOLD; + case JESTER: + return ChatColor.LIGHT_PURPLE; + case INNOCENT: + return ChatColor.GREEN; + } + return ChatColor.MAGIC; + } + +} diff --git a/src/main/java/com/the5gi/util/RoleDescriptionHelper.java b/src/main/java/com/the5gi/util/RoleDescriptionHelper.java new file mode 100644 index 0000000..c52916d --- /dev/null +++ b/src/main/java/com/the5gi/util/RoleDescriptionHelper.java @@ -0,0 +1,29 @@ +package com.the5gi.util; + +public class RoleDescriptionHelper { + + public static String getRoleDescription(Role role) { + switch (role) { + case DETECTIVE: + return "Kill all the traitors to win with the innocents! But everyone can see who you are!"; + case SHERIFF: + return "Kill all the traitors to win with the innocents! But everyone can see who you are (Better Detective)!"; + case SURVIVALISTS: + return "You are an Innocent Role: Kill all the traitors to win with the innocents! You can buy detective weapons/items!"; + case MERCENARIES: + return "You are an Innocent Role: Kill all the traitors to win with the innocents! You can but Traitor Weapons so be careful!"; + case TRAITORS: + return "You are a Traitor Role: Kill all the innocents/neutral roles to win! You have access to special items!"; + case ASSASSINS: + return "You are a Traitor Role: Kill your targets to win by yourself, or help the normal Traitors if your target dies (Neutral Traitor)"; + case LONEWOLF: + return "You are a Neutral Role: Kill everyone to win! You can buy things like the survivalist! You are on your own team!"; + case JESTER: + return "You are a Neutral Role: Get killed by an Innocent Role to win! If all the traitors are dead, you can attack and vice versa with Innocents!"; + case INNOCENT: + return "You are an Innocent Role: Kill all the traitors to win!"; + } + return "Error"; + } + +} diff --git a/src/main/java/com/the5gi/util/RoleWinnerHelper.java b/src/main/java/com/the5gi/util/RoleWinnerHelper.java new file mode 100644 index 0000000..2f5cb5d --- /dev/null +++ b/src/main/java/com/the5gi/util/RoleWinnerHelper.java @@ -0,0 +1,19 @@ +package com.the5gi.util; + +public class RoleWinnerHelper { + public static String getTitle(Role role) { + switch (role) { + case INNOCENT: + return "The Innocents Win!"; + case TRAITORS: + return "The Traitors Win!"; + case ASSASSINS: + return "The Assassin Wins!"; + case JESTER: + return "The Jester Wins!"; + case LONEWOLF: + return "The Lone Wolf Wins!"; + } + return "Error!"; + } +} diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml new file mode 100644 index 0000000..a1dbfb5 --- /dev/null +++ b/src/main/resources/plugin.yml @@ -0,0 +1,10 @@ +name: SimpleTTT +version: 1.0.0 +main: com.the5gi.SimpleTTTCore +api-version: 1.19 +authors: [ the5gi,SteveO718 ] +commands: + startgame: + permission: sttt.start + usage: /startgame + description: starts the game \ No newline at end of file