Created
September 25, 2020 19:50
-
-
Save Buchstabet/c3bbfec94e825ec75efa8db4538c6019 to your computer and use it in GitHub Desktop.
TicTacToe
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package de.buchstabet.tictactoe.de.buchstabet.tictactoe.match; | |
import de.buchstabet.tictactoe.main.Main; | |
import de.buchstabet.tictactoe.utils.Utils; | |
import org.bukkit.Bukkit; | |
import org.bukkit.DyeColor; | |
import org.bukkit.Location; | |
import org.bukkit.Material; | |
import org.bukkit.block.Block; | |
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.block.BlockPlaceEvent; | |
import org.bukkit.event.inventory.InventoryClickEvent; | |
import org.bukkit.event.inventory.InventoryCloseEvent; | |
import org.bukkit.event.player.PlayerInteractEvent; | |
import org.bukkit.inventory.ItemStack; | |
import org.bukkit.inventory.meta.ItemMeta; | |
public class Manager implements Listener { | |
@EventHandler | |
public void onInvClick(InventoryClickEvent e) { | |
for (Match match : Utils.matches) { | |
if (match.inventory == e.getInventory()) { | |
e.setCancelled(true); | |
Player player = (Player) e.getWhoClicked(); | |
Player versus = null; | |
if (match.player1 == player) | |
versus = match.player2; | |
else | |
versus = match.player1; | |
if (versus == null) { | |
match.player1.closeInventory(); | |
match.player2.closeInventory(); | |
Utils.matches.remove(match); | |
return; | |
} | |
int type = 1; | |
if (player == match.player2) | |
type = 2; | |
//Inventar Manager | |
if (match.currentPlaying == type) { | |
try { | |
if (e.getCurrentItem().getType() == Material.STAINED_GLASS_PANE) { | |
e.getInventory().setItem(e.getSlot(), getColor(type, player)); | |
ItemStack mat1 = match.inventory.getItem(0); | |
ItemStack mat2 = match.inventory.getItem(1); | |
ItemStack mat3 = match.inventory.getItem(2); | |
ItemStack mat4 = match.inventory.getItem(3); | |
ItemStack mat5 = match.inventory.getItem(4); | |
ItemStack mat6 = match.inventory.getItem(5); | |
ItemStack mat7 = match.inventory.getItem(6); | |
ItemStack mat8 = match.inventory.getItem(7); | |
ItemStack mat9 = match.inventory.getItem(8); | |
if (mat1.getItemMeta().getDisplayName().equals(player.getName()) && mat4.getItemMeta().getDisplayName().equals(player.getName()) && mat7.getItemMeta().getDisplayName().equals(player.getName())) { | |
setWinner(player, match); | |
return; | |
} | |
if (mat2.getItemMeta().getDisplayName().equals(player.getName()) && mat5.getItemMeta().getDisplayName().equals(player.getName()) && mat8.getItemMeta().getDisplayName().equals(player.getName())) { | |
setWinner(player, match); | |
return; | |
} | |
if (mat3.getItemMeta().getDisplayName().equals(player.getName()) && mat6.getItemMeta().getDisplayName().equals(player.getName()) && mat9.getItemMeta().getDisplayName().equals(player.getName())) { | |
setWinner(player, match); | |
return; | |
} | |
if (mat1.getItemMeta().getDisplayName().equals(player.getName()) && mat2.getItemMeta().getDisplayName().equals(player.getName()) && mat3.getItemMeta().getDisplayName().equals(player.getName())) { | |
setWinner(player, match); | |
return; | |
} | |
if (mat4.getItemMeta().getDisplayName().equals(player.getName()) && mat5.getItemMeta().getDisplayName().equals(player.getName()) && mat6.getItemMeta().getDisplayName().equals(player.getName())) { | |
setWinner(player, match); | |
return; | |
} | |
if (mat7.getItemMeta().getDisplayName().equals(player.getName()) && mat8.getItemMeta().getDisplayName().equals(player.getName()) && mat9.getItemMeta().getDisplayName().equals(player.getName())) { | |
setWinner(player, match); | |
return; | |
} | |
if (mat1.getItemMeta().getDisplayName().equals(player.getName()) && mat5.getItemMeta().getDisplayName().equals(player.getName()) && mat9.getItemMeta().getDisplayName().equals(player.getName())) { | |
setWinner(player, match); | |
return; | |
} | |
if (mat3.getItemMeta().getDisplayName().equals(player.getName()) && mat5.getItemMeta().getDisplayName().equals(player.getName()) && mat7.getItemMeta().getDisplayName().equals(player.getName())) { | |
setWinner(player, match); | |
return; | |
} | |
boolean finish = true; | |
for (int i = 0; i < 9; i++) { | |
if (match.inventory.getItem(i).getType() == Material.STAINED_GLASS_PANE) | |
finish = false; | |
} | |
if (finish) { | |
Utils.matches.remove(match); | |
if (match.player2 != null) { | |
match.player2.closeInventory(); | |
match.player2.sendMessage("§eUnentschieden!"); | |
} | |
if (match.player1 != null) { | |
match.player1.closeInventory(); | |
match.player1.sendMessage("§eUnentschieden!"); | |
} | |
return; | |
} | |
if (match.currentPlaying == 1) | |
match.currentPlaying = 2; | |
else | |
match.currentPlaying = 1; | |
} | |
} catch (Exception ignored) { | |
} | |
} else | |
player.sendMessage("§cDu bist nicht dran!"); | |
} | |
} | |
} | |
public static void setWinner(Player player, Match match) { | |
player.sendMessage("§aDu hast gewonnen!"); | |
if (match.player1 == player) { | |
if (match.player2 != null) | |
match.player2.sendMessage("§cDu hast verloren!"); | |
} else { | |
if (match.player1 != null) | |
match.player1.sendMessage("§cDu hast verloren!"); | |
} | |
Utils.matches.remove(match); | |
if (match.player2 != null) | |
match.player2.closeInventory(); | |
if (match.player1 != null) | |
match.player1.closeInventory(); | |
} | |
private ItemStack getColor(int type, Player player) { | |
if (type == 1) { | |
ItemStack stack = new ItemStack(Material.WOOL, 0, DyeColor.RED.getData()); | |
ItemMeta meta = stack.getItemMeta(); | |
meta.setDisplayName(player.getName()); | |
stack.setItemMeta(meta); | |
return stack; | |
} else { | |
ItemStack stack = new ItemStack(Material.WOOL, 0, DyeColor.BLUE.getData()); | |
ItemMeta meta = stack.getItemMeta(); | |
meta.setDisplayName(player.getName()); | |
stack.setItemMeta(meta); | |
return stack; | |
} | |
} | |
@EventHandler | |
public void onClose(InventoryCloseEvent e) { | |
if (Utils.matches.size() != 0) { | |
for (Match match : Utils.matches) { | |
Utils.matches.remove(match); | |
if (match.isCosed) | |
return; | |
else | |
match.isCosed = true; | |
if (match.inventory == e.getInventory()) { | |
if (match.player2 != null) | |
match.player2.closeInventory(); | |
if (match.player1 != null) | |
match.player1.closeInventory(); | |
if (match.player2 == e.getPlayer()) { | |
if (match.player1 != null) | |
match.player1.sendMessage("§aDu hast gewonnen!"); | |
if (match.player2 != null) | |
match.player2.sendMessage("§cDu hast verloren!"); | |
} | |
if (match.player1 == e.getPlayer()) { | |
if (match.player2 != null) | |
match.player2.sendMessage("§aDu hast gewonnen!"); | |
if (match.player1 != null) | |
match.player1.sendMessage("§cDu hast verloren!"); | |
} | |
} | |
} | |
} | |
} | |
@EventHandler | |
public void onBlockPlace(BlockPlaceEvent e) { | |
if(SetBlock.setBlock.contains(e.getPlayer())) { | |
if(e.getBlock().getType() == Main.block) { | |
Main.getPlugin(Main.class).getConfig().set("Location", e.getBlock().getLocation()); | |
Main.getPlugin(Main.class).saveConfig(); | |
SetBlock.setBlock.remove(e.getPlayer()); | |
e.getPlayer().sendMessage("§aDu hast den Block gesetzt!"); | |
} | |
} | |
} | |
@EventHandler | |
public void onInteract(PlayerInteractEvent e) { | |
if(e.getAction() == Action.RIGHT_CLICK_BLOCK) { | |
Block block = e.getClickedBlock(); | |
Player player = e.getPlayer(); | |
int xBlock = block.getX(); | |
int yBlock = block.getY(); | |
int zBlock = block.getZ(); | |
int x = block.getWorld().getBlockAt(Main.location).getX(); | |
int y = block.getWorld().getBlockAt(Main.location).getY(); | |
int z = block.getWorld().getBlockAt(Main.location).getZ(); | |
if (xBlock == x && yBlock == y && zBlock == z) { | |
e.setCancelled(true); | |
for (Match match : Utils.matches) { | |
if(match.player1 != null && match.player1 == player) { | |
player.sendMessage("§cDu bist schon in einem Match"); | |
return; | |
} | |
if(match.player2 != null && match.player2 == player) { | |
player.sendMessage("§cDu bist schon in einem Match"); | |
return; | |
} | |
} | |
if(Utils.waitingPlayers.contains(player)) { | |
player.sendMessage("§cDu hast die Warteschlange verlassen!"); | |
Utils.waitingPlayers.remove(player); | |
}else { | |
player.sendMessage("§aDu hast die Warteschlange betreten!"); | |
Utils.waitingPlayers.add(player); | |
} | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package de.buchstabet.tictactoe.de.buchstabet.tictactoe.match; | |
import org.bukkit.Bukkit; | |
import org.bukkit.Material; | |
import org.bukkit.entity.Player; | |
import org.bukkit.event.inventory.InventoryType; | |
import org.bukkit.inventory.Inventory; | |
import org.bukkit.inventory.ItemStack; | |
import org.bukkit.inventory.meta.ItemMeta; | |
public class Match { | |
Player player1; | |
Player player2; | |
int currentPlaying; | |
boolean isCosed; | |
Inventory inventory; | |
public Match(Player p1, Player p2) { | |
isCosed = false; | |
player1 = p1; | |
player2 = p2; | |
currentPlaying = 1; | |
inventory = Bukkit.createInventory(null, InventoryType.DISPENSER, "§cTicTacToe"); | |
ItemStack stack = new ItemStack(Material.STAINED_GLASS_PANE); | |
ItemMeta meta = stack.getItemMeta(); | |
meta.setDisplayName("§3"); | |
stack.setItemMeta(meta); | |
for (int i = 0; i < 9; i++) | |
inventory.setItem(i, stack); | |
player1.openInventory(inventory); | |
player2.openInventory(inventory); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package de.buchstabet.tictactoe.de.buchstabet.tictactoe.match; | |
import de.buchstabet.tictactoe.utils.Utils; | |
import org.bukkit.command.Command; | |
import org.bukkit.command.CommandExecutor; | |
import org.bukkit.command.CommandSender; | |
import org.bukkit.entity.Player; | |
public class Queue implements CommandExecutor { | |
@Override | |
public boolean onCommand(CommandSender sender, Command command, String s, String[] args) { | |
if(sender instanceof Player) { | |
Player player = (Player) sender; | |
for (Match match : Utils.matches) { | |
if(match.player1 != null && match.player1 == player) { | |
player.sendMessage("§cDu bist schon in einem Match"); | |
return true; | |
} | |
if(match.player2 != null && match.player2 == player) { | |
player.sendMessage("§cDu bist schon in einem Match"); | |
return true; | |
} | |
} | |
if(Utils.waitingPlayers.contains(player)) { | |
player.sendMessage("§cDu hast die Warteschlange verlassen!"); | |
Utils.waitingPlayers.remove(player); | |
}else { | |
player.sendMessage("§aDu hast die Warteschlange betreten!"); | |
Utils.waitingPlayers.add(player); | |
} | |
} | |
return false; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package de.buchstabet.tictactoe.de.buchstabet.tictactoe.match; | |
import org.bukkit.command.Command; | |
import org.bukkit.command.CommandExecutor; | |
import org.bukkit.command.CommandSender; | |
import org.bukkit.entity.Player; | |
import java.util.ArrayList; | |
public class SetBlock implements CommandExecutor { | |
public static ArrayList<Player> setBlock = new ArrayList<>(); | |
@Override | |
public boolean onCommand(CommandSender sender, Command command, String s, String[] args) { | |
if(sender instanceof Player) { | |
Player player = (Player) sender; | |
if(player.hasPermission("TicTacToe.setBlock")) { | |
if(setBlock.contains(player)) { | |
setBlock.remove(player); | |
player.sendMessage("§cSetzte nun nicht mehr den Block!"); | |
}else { | |
player.sendMessage("§aSetzte nun den Block!"); | |
setBlock.add(player); | |
} | |
}else | |
player.sendMessage("§cKeine Rechte!"); | |
} | |
return false; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package de.buchstabet.tictactoe.main; | |
import de.buchstabet.tictactoe.de.buchstabet.tictactoe.match.Manager; | |
import de.buchstabet.tictactoe.de.buchstabet.tictactoe.match.Queue; | |
import de.buchstabet.tictactoe.de.buchstabet.tictactoe.match.SetBlock; | |
import de.buchstabet.tictactoe.utils.Utils; | |
import org.bukkit.Bukkit; | |
import org.bukkit.Location; | |
import org.bukkit.Material; | |
import org.bukkit.plugin.PluginManager; | |
import org.bukkit.plugin.java.JavaPlugin; | |
import java.io.IOException; | |
public class Main extends JavaPlugin { | |
public static Material block; | |
public static Location location; | |
@Override | |
public void onEnable() { | |
Utils.startScheduler(this); | |
PluginManager manager = Bukkit.getPluginManager(); | |
manager.registerEvents(new Manager(), this); | |
getCommand("Queue").setExecutor(new Queue()); | |
getCommand("getTicTacToeBlock").setExecutor(new SetBlock()); | |
if(!getFile().exists()) { | |
try { | |
getFile().createNewFile(); | |
} catch (IOException exception) { | |
exception.printStackTrace(); | |
} | |
} | |
getConfig().options().copyDefaults(true); | |
getConfig().addDefault("BlockType", Material.WORKBENCH.name()); | |
getConfig().addDefault("Location", new Location(Bukkit.getWorld("world"), 0, 0, 0)); | |
saveConfig(); | |
location = (Location) getConfig().get("Location"); | |
try{ | |
block = Material.valueOf(getConfig().getString("BlockType")); | |
Bukkit.getConsoleSender().sendMessage("§aDer BlockType ist: " + block.name()); | |
}catch (Exception e) { | |
Bukkit.getConsoleSender().sendMessage("§cDer BlockType ist falsch!"); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package de.buchstabet.tictactoe.utils; | |
import de.buchstabet.tictactoe.de.buchstabet.tictactoe.match.Match; | |
import de.buchstabet.tictactoe.main.Main; | |
import org.bukkit.Bukkit; | |
import org.bukkit.entity.Player; | |
import java.util.ArrayList; | |
import java.util.Collections; | |
public class Utils { | |
public static ArrayList<Player> waitingPlayers = new ArrayList<>(); | |
public static ArrayList<Match> matches = new ArrayList<>(); | |
public static void startScheduler(Main plugin) { | |
Bukkit.getScheduler().scheduleSyncRepeatingTask(plugin, new Runnable() { | |
@Override | |
public void run() { | |
if(waitingPlayers.size() >= 2) { | |
Collections.shuffle(waitingPlayers); | |
//new Match | |
Match match = new Match(waitingPlayers.remove(0), waitingPlayers.remove(0)); | |
matches.add(match); | |
} | |
} | |
}, 0, 2); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
main: de.buchstabet.tictactoe.main.Main | |
version: 1.0 | |
author: Buchstabet | |
name: TicTacToe | |
commands: | |
Queue: | |
getTicTacToeBlock: |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment