Created
March 12, 2022 00:05
-
-
Save MWHunter/0a53f3b307120d2de9e1bc13ef9fc2ae to your computer and use it in GitHub Desktop.
bukkit game jam
This file contains hidden or 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 me.defineoutside.tntspleef; | |
import org.bukkit.*; | |
import org.bukkit.block.Block; | |
import org.bukkit.command.Command; | |
import org.bukkit.command.CommandExecutor; | |
import org.bukkit.command.CommandSender; | |
import org.bukkit.enchantments.Enchantment; | |
import org.bukkit.entity.EntityType; | |
import org.bukkit.entity.Player; | |
import org.bukkit.entity.TNTPrimed; | |
import org.bukkit.event.EventHandler; | |
import org.bukkit.event.Listener; | |
import org.bukkit.event.entity.EntityShootBowEvent; | |
import org.bukkit.event.entity.PlayerDeathEvent; | |
import org.bukkit.event.player.PlayerJoinEvent; | |
import org.bukkit.event.player.PlayerMoveEvent; | |
import org.bukkit.inventory.ItemStack; | |
import org.bukkit.inventory.meta.ItemMeta; | |
import org.bukkit.plugin.java.JavaPlugin; | |
import org.bukkit.potion.PotionEffect; | |
import org.bukkit.potion.PotionEffectType; | |
import org.bukkit.scheduler.BukkitTask; | |
import java.util.Random; | |
import java.util.concurrent.atomic.AtomicInteger; | |
public final class TntSpleef extends JavaPlugin implements CommandExecutor, Listener { | |
GameState currentState = GameState.NOT_STARTED; | |
@Override | |
public void onEnable() { | |
// Plugin startup logic | |
Bukkit.getPluginManager().registerEvents(this, this); | |
Material ONE = Material.YELLOW_STAINED_GLASS; | |
Material TWO = Material.RED_STAINED_GLASS; | |
final Random random = new Random(); | |
Bukkit.getScheduler().runTaskTimer(this, () -> { | |
World world = Bukkit.getWorld("world"); | |
if (currentState != GameState.IN_GAME) return; | |
for (int i = 0; i < 32; i++) { | |
final int x = random.nextInt(200) - 100; | |
final int z = random.nextInt(200) - 100; | |
final int y = world.getHighestBlockYAt(x, z); | |
if (y >= 25) { | |
Block block = world.getBlockAt(x, y, z); | |
if (block.getType() == ONE) { | |
for (int setY = 0; setY < 255; setY++) { | |
if (world.getBlockAt(x, setY, z).getType() != Material.AIR) { | |
world.getBlockAt(x, setY, z).setType(TWO); | |
} | |
} | |
} else if (block.getType() == TWO) { | |
for (int setY = 0; setY < 255; setY++) { | |
if (world.getBlockAt(x, setY, z).getType() != Material.AIR) { | |
world.getBlockAt(x, setY, z).setType(Material.AIR); | |
} | |
} | |
} else { | |
for (int setY = 0; setY < 255; setY++) { | |
if (world.getBlockAt(x, setY, z).getType() != Material.AIR) { | |
world.getBlockAt(x, setY, z).setType(ONE); | |
} | |
} | |
} | |
} | |
} | |
}, 1, 1); | |
} | |
@Override | |
public void onDisable() { | |
// Plugin shutdown logic | |
} | |
@EventHandler | |
public void onPlayerJoin(PlayerJoinEvent event) { | |
tpPlayer(event.getPlayer()); | |
} | |
@EventHandler | |
public void onPlayerMove(PlayerMoveEvent event) { | |
if (event.getTo().getY() < 0) { | |
killPlayer(event.getPlayer()); | |
} | |
} | |
@EventHandler | |
public void onPlayerDeath(PlayerDeathEvent event) { | |
killPlayer(event.getEntity()); | |
event.setCancelled(true); | |
} | |
private void tpPlayer(Player player) { | |
player.getPlayer().setHealth(20); | |
for (PotionEffect effect : player.getActivePotionEffects()) { | |
player.removePotionEffect(effect.getType()); | |
} | |
player.getPlayer().teleport(new Location(Bukkit.getWorld("world"), 0, 100, 0)); | |
player.getPlayer().setGameMode(GameMode.SPECTATOR); | |
player.getPlayer().addPotionEffect(new PotionEffect(PotionEffectType.BLINDNESS, 40, 1)); | |
} | |
private void killPlayer(Player player) { | |
tpPlayer(player); | |
int alivePlayers = 0; | |
Player lastAlive = null; | |
for (Player p : Bukkit.getOnlinePlayers()) { | |
if (p.getGameMode() == GameMode.ADVENTURE) { | |
alivePlayers++; | |
lastAlive = p; | |
} | |
} | |
if (alivePlayers <= 1) { | |
Bukkit.broadcastMessage("The game has ended, " + (lastAlive == null ? "null?" : lastAlive.getName()) + " won!"); | |
currentState = GameState.NOT_STARTED; | |
for (Player p : Bukkit.getOnlinePlayers()) { | |
if (p.getGameMode() == GameMode.SURVIVAL) { | |
tpPlayer(p); | |
} | |
} | |
} | |
} | |
@Override | |
public boolean onCommand(final CommandSender sender, final Command command, final String label, final String[] args) { | |
if (label.equalsIgnoreCase("startgame")) { | |
Bukkit.broadcastMessage("Starting game!"); | |
currentState = GameState.COUNTDOWN; | |
AtomicInteger count = new AtomicInteger(5); | |
BukkitTask task = Bukkit.getScheduler().runTaskTimer(this, () -> { | |
if (count.getAndDecrement() == 0) { | |
currentState = GameState.IN_GAME; | |
regenArena(); | |
teleportPlayers(); | |
} else { | |
Bukkit.broadcastMessage(count.get() + "..."); | |
} | |
}, 20, 20); | |
Bukkit.getScheduler().runTaskLater(this, () -> { | |
Bukkit.getScheduler().cancelTask(task.getTaskId()); | |
}, 120); | |
} | |
return true; | |
} | |
@EventHandler | |
public void onPlayerShoot(EntityShootBowEvent event) { | |
if (event.getEntity() instanceof Player) { | |
Player player = (Player) event.getEntity(); | |
Location location = player.getEyeLocation(); | |
TNTPrimed primed = (TNTPrimed) location.getWorld().spawnEntity(location, EntityType.PRIMED_TNT); | |
primed.setFuseTicks(30); | |
primed.setVelocity(location.getDirection().multiply(2)); | |
} | |
} | |
public void regenArena() { | |
for (int x = -27; x <= 91; ++x) { | |
for (int y = 55; y <= 83; ++y) { | |
for (int z = -53; z < 37; ++z) { | |
final Location location = new Location(Bukkit.getWorld("world"), x, y, z + 400); | |
final Location paste = new Location(Bukkit.getWorld("world"), x, y, z); | |
paste.getBlock().setBlockData(location.getBlock().getBlockData()); | |
} | |
} | |
} | |
} | |
public void teleportPlayers() { | |
for (final Player player : Bukkit.getServer().getOnlinePlayers()) { | |
player.setGameMode(GameMode.ADVENTURE); | |
player.getInventory().clear(); | |
final ItemStack bow = new ItemStack(Material.BOW, 1); | |
ItemMeta meta = bow.getItemMeta(); | |
meta.addEnchant(Enchantment.ARROW_INFINITE, 1, true); | |
bow.setItemMeta(meta); | |
ItemStack arrow = new ItemStack(Material.ARROW, 1); | |
player.getInventory().addItem(bow); | |
player.getInventory().addItem(arrow); | |
player.getInventory().setHelmet(new ItemStack(Material.IRON_HELMET)); | |
player.getInventory().setChestplate(new ItemStack(Material.IRON_CHESTPLATE)); | |
player.getInventory().setLeggings(new ItemStack(Material.IRON_LEGGINGS)); | |
player.getInventory().setBoots(new ItemStack(Material.IRON_BOOTS)); | |
player.addPotionEffect(new PotionEffect(PotionEffectType.ABSORPTION, 1000000, 2)); | |
player.addPotionEffect(new PotionEffect(PotionEffectType.REGENERATION, 1000000, 1)); | |
player.addPotionEffect(new PotionEffect(PotionEffectType.SPEED, 1000000, 1)); | |
player.addPotionEffect(new PotionEffect(PotionEffectType.JUMP, 1000000, 1)); | |
final Random random = new Random(); | |
Location teleportLocation; | |
while (true) { | |
final int x = random.nextInt(100) - 50; | |
final int z = random.nextInt(100) - 50; | |
final int y = player.getWorld().getHighestBlockYAt(x, z) + 1; | |
if (y >= 25) { // Magic value, below this would be void | |
teleportLocation = new Location(player.getWorld(), x, y, z); | |
player.teleport(teleportLocation); | |
player.sendMessage(ChatColor.GREEN + "The Game has Begun"); | |
break; | |
} | |
} | |
} | |
} | |
public enum GameState { | |
NOT_STARTED, | |
COUNTDOWN, | |
IN_GAME | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment