A good base for an island highlighting map.

This commit is contained in:
Harrison Deng 2020-05-13 12:07:16 -05:00
parent 93ea0d3153
commit 78f0a846da
17 changed files with 381 additions and 77 deletions

View File

@ -2,13 +2,17 @@ package ca.recrown.islandsurvivalcraft;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.HandlerList;
import org.bukkit.event.Listener;
import org.bukkit.event.server.ServerLoadEvent;
import org.bukkit.generator.ChunkGenerator;
import org.bukkit.plugin.PluginManager;
import org.bukkit.plugin.java.JavaPlugin;
import ca.recrown.islandsurvivalcraft.interaction.commands.CommandProcessor;
import ca.recrown.islandsurvivalcraft.interaction.items.VariedItemManager;
import ca.recrown.islandsurvivalcraft.world.WorldInfoManager;
import ca.recrown.islandsurvivalcraft.world.generation.GeneratorModes;
@ -16,23 +20,34 @@ public class IslandSurvivalCraft extends JavaPlugin implements Listener {
private PluginManager pluginManager;
private WorldInfoManager worldInfoManager;
private CommandProcessor commandProcessor;
private VariedItemManager variedItemManager;
public IslandSurvivalCraft() {
worldInfoManager = new WorldInfoManager();
variedItemManager = new VariedItemManager(this);
commandProcessor = new CommandProcessor(this);
}
@Override
public void onEnable() {
pluginManager = getServer().getPluginManager();
pluginManager.registerEvents(worldInfoManager, this);
commandProcessor = new CommandProcessor(this);
pluginManager.registerEvents(variedItemManager, this);
pluginManager.registerEvents(this, this);
super.onEnable();
}
@EventHandler(priority = EventPriority.MONITOR)
public void onReady(ServerLoadEvent e) {
variedItemManager.loadAllItems();
}
@Override
public void onDisable() {
HandlerList.unregisterAll(worldInfoManager);
commandProcessor = null;
HandlerList.unregisterAll(variedItemManager);
HandlerList.unregisterAll((Listener) this);
variedItemManager.unloadAllItems();
super.onDisable();
}
@ -47,6 +62,13 @@ public class IslandSurvivalCraft extends JavaPlugin implements Listener {
return worldInfoManager.getChunkGenerator(worldName, gID);
}
@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
return commandProcessor.onCommand(sender, command, label, args);
}
/**
* @return the world information manager.
*/
@ -54,8 +76,4 @@ public class IslandSurvivalCraft extends JavaPlugin implements Listener {
return worldInfoManager;
}
@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
return commandProcessor.onCommand(sender, command, label, args);
}
}

View File

@ -1,7 +1,6 @@
package ca.recrown.islandsurvivalcraft.interaction.commands;
import java.util.Arrays;
import java.util.HashSet;
import org.bukkit.ChatColor;
import org.bukkit.command.Command;
@ -12,7 +11,6 @@ import ca.recrown.islandsurvivalcraft.IslandSurvivalCraft;
public class CommandProcessor implements CommandExecutor {
private IslandSurvivalCraft islandSurvivalCraft;
private HashSet<Commands> initializedCommands = new HashSet<>();
public CommandProcessor(IslandSurvivalCraft islandSurvivalCraft) {
this.islandSurvivalCraft = islandSurvivalCraft;
@ -21,15 +19,15 @@ public class CommandProcessor implements CommandExecutor {
@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
if (args.length < 1 || args[0].isEmpty()) return false;
Commands currentCommand = null;
RegisteredCommands currentCommand = null;
try {
currentCommand = Commands.valueOf(args[0].toUpperCase());
currentCommand = RegisteredCommands.valueOf(args[0].toUpperCase());
} catch (IllegalArgumentException e) {
sender.sendMessage(ChatColor.LIGHT_PURPLE + "This command was not understood. Refer to \"/IslandSurvivalCraft help\" for more info.");
}
if (initializedCommands.add(currentCommand)) currentCommand.initialize(islandSurvivalCraft);
if (currentCommand.getCommandRunnable().requiresInitialization()) currentCommand.getCommandRunnable().initialize(islandSurvivalCraft);
String[] commandArguments = Arrays.copyOfRange(args, 1, args.length);
boolean success = currentCommand.invoke(sender, commandArguments);
boolean success = currentCommand.getCommandRunnable().invoke(sender, commandArguments);
if (!success) sender.sendMessage(String.format("The command arguments were incorrect. Please refer to \"/IslandSurvivalCraft help %s\" for help.", currentCommand.toString()));
return true;
}

View File

@ -1,50 +0,0 @@
package ca.recrown.islandsurvivalcraft.interaction.commands;
import ca.recrown.islandsurvivalcraft.interaction.commands.runnables.HighlightIslandCommand;
import org.bukkit.command.CommandSender;
import ca.recrown.islandsurvivalcraft.IslandSurvivalCraft;
import ca.recrown.islandsurvivalcraft.interaction.commands.runnables.*;
public enum Commands implements CommandRunnable {
HIGHLIGHT(new HighlightIslandCommand()),
HELP(new HelpCommand()),
VALUE(new ValueRunnable()),
;
private final CommandRunnable commandRunnable;
private boolean initialized = false;
private Commands(CommandRunnable runnable) {
this.commandRunnable = runnable;
}
@Override
public String toString() {
return name().toLowerCase();
}
@Override
public boolean invoke(CommandSender sender, String[] args) {
return commandRunnable.invoke(sender, args);
}
@Override
public void initialize(IslandSurvivalCraft islandSurvivalCraft) {
if (initialized) throw new IllegalStateException("Command already initialized.");
initialized = true;
commandRunnable.initialize(islandSurvivalCraft);
}
@Override
public String getDescription() {
return commandRunnable.getDescription();
}
@Override
public String getDetailedDescription() {
return commandRunnable.getDetailedDescription();
}
}

View File

@ -0,0 +1,30 @@
package ca.recrown.islandsurvivalcraft.interaction.commands;
import ca.recrown.islandsurvivalcraft.interaction.commands.runnables.HighlightIslandCommand;
import ca.recrown.islandsurvivalcraft.interaction.commands.runnables.*;
public enum RegisteredCommands {
HIGHLIGHT(new HighlightIslandCommand()),
HELP(new HelpCommand()),
VALUE(new ValueRunnable()),
;
private final CommandRunnable commandRunnable;
private RegisteredCommands(CommandRunnable runnable) {
this.commandRunnable = runnable;
}
@Override
public String toString() {
return name().toLowerCase();
}
/**
* @return the commandRunnable
*/
public CommandRunnable getCommandRunnable() {
return commandRunnable;
}
}

View File

@ -29,4 +29,10 @@ public interface CommandRunnable {
* @param islandSurvivalCraft
*/
public void initialize(IslandSurvivalCraft islandSurvivalCraft);
/**
*
* @return True if on the next call, should initialize.
*/
public boolean requiresInitialization();
}

View File

@ -4,7 +4,7 @@ import org.bukkit.ChatColor;
import org.bukkit.command.CommandSender;
import ca.recrown.islandsurvivalcraft.IslandSurvivalCraft;
import ca.recrown.islandsurvivalcraft.interaction.commands.Commands;
import ca.recrown.islandsurvivalcraft.interaction.commands.RegisteredCommands;
public class HelpCommand implements CommandRunnable {
private String helpMessage;
@ -19,18 +19,18 @@ public class HelpCommand implements CommandRunnable {
sender.sendMessage(helpMessage);
return true;
} else {
Commands command = null;
RegisteredCommands command = null;
try {
command = Commands.valueOf(args[0].toUpperCase());
command = RegisteredCommands.valueOf(args[0].toUpperCase());
} catch (IllegalArgumentException e) {
return false;
}
StringBuilder sb = new StringBuilder();
sb.append(ChatColor.YELLOW + args[0]);
sb.append(": " + ChatColor.GRAY);
sb.append(command.getDescription());
sb.append(command.getCommandRunnable().getDescription());
sb.append("\n" + ChatColor.WHITE);
sb.append(command.getDetailedDescription());
sb.append(command.getCommandRunnable().getDetailedDescription());
sender.sendMessage(sb.toString().trim());
}
return true;
@ -39,12 +39,12 @@ public class HelpCommand implements CommandRunnable {
@Override
public void initialize(IslandSurvivalCraft islandSurvivalCraft) {
StringBuilder stringBuilder = new StringBuilder();
Commands[] commands = Commands.values();
RegisteredCommands[] commands = RegisteredCommands.values();
stringBuilder.append(ChatColor.YELLOW + "IslandSurvivalCraft Commands:\n");
for (int i = 0; i < commands.length; i++) {
stringBuilder.append(ChatColor.GREEN + commands[i].toString());
stringBuilder.append(": " + ChatColor.WHITE);
stringBuilder.append(commands[i].getDescription());
stringBuilder.append(commands[i].getCommandRunnable().getDescription());
stringBuilder.append("\n");
}
helpMessage = stringBuilder.toString().trim();
@ -55,4 +55,9 @@ public class HelpCommand implements CommandRunnable {
return " \"islandsurvivalcraft help [command]\". Where [command] is a listed command, can optionally be added to get a detailed look at the command. Help is a command that lists all the other potential commands that may be run. Can also look at the detailed commands. But you knew this since your using help with help...";
}
@Override
public boolean requiresInitialization() {
return helpMessage == null;
}
}

View File

@ -19,7 +19,9 @@ import ca.recrown.islandsurvivalcraft.world.WorldInfo;
import ca.recrown.islandsurvivalcraft.world.Information.IslandInformation;
public class HighlightIslandCommand implements CommandRunnable {
private final Particle.DustOptions OPTIONS = new Particle.DustOptions(Color.RED, 1f);
private boolean requiresInit = true;
private final Particle.DustOptions EDGE_DUST_OPTIONS = new Particle.DustOptions(Color.RED, 1f);
private final Particle.DustOptions ORIGIN_DUST_OPTIONS = new Particle.DustOptions(Color.GREEN, 1f);
private final int PARTICLE_AMOUNT = 1;
private final double OFFSET_X = 0.0d, OFFSET_Y = 6.0d, OFFSET_Z = 0.0d;
private final double EXTRA = 0d;
@ -62,6 +64,7 @@ public class HighlightIslandCommand implements CommandRunnable {
@Override
public void initialize(IslandSurvivalCraft islandsurvivalcraft) {
requiresInit = false;
Bukkit.getScheduler().scheduleSyncRepeatingTask(islandsurvivalcraft, new Runnable() {
@Override
public void run() {
@ -81,9 +84,10 @@ public class HighlightIslandCommand implements CommandRunnable {
Iterator<Point2> islandborderIterable = islandBorder.iterator();
for (int i = 0; i < islandBorder.size(); i+= 2) {
Point2 current = islandborderIterable.next();
spawnParticle(world, current.x, playerY, current.y);
spawnParticle(world, current.x, playerY, current.y, EDGE_DUST_OPTIONS);
islandborderIterable.next();
}
spawnParticle(world, islandInfo.getIslandOrigin().x, playerY, islandInfo.getIslandOrigin().y, ORIGIN_DUST_OPTIONS);
}
waitingList.add(player);
}
@ -91,12 +95,17 @@ public class HighlightIslandCommand implements CommandRunnable {
}
private void spawnParticle(World world, double x, double y, double z) {
world.spawnParticle(Particle.REDSTONE, x, y, z, PARTICLE_AMOUNT, OFFSET_X, OFFSET_Y, OFFSET_Z, EXTRA, OPTIONS, false);
private void spawnParticle(World world, double x, double y, double z, Particle.DustOptions dustOptions) {
world.spawnParticle(Particle.REDSTONE, x, y, z, PARTICLE_AMOUNT, OFFSET_X, OFFSET_Y, OFFSET_Z, EXTRA, dustOptions, false);
}
@Override
public String getDetailedDescription() {
return "A helpful debugging command to analyze what the plugin considers an island by displaying particle effects around the player that is a part of the island. The <start | stop> argument is required to dicate whether or not to start the highlighting or stop it.";
}
@Override
public boolean requiresInitialization() {
return requiresInit;
}
}

View File

@ -61,4 +61,9 @@ public class ValueRunnable implements CommandRunnable {
isc = islandSurvivalCraft;
}
@Override
public boolean requiresInitialization() {
return isc == null;
}
}

View File

@ -0,0 +1,110 @@
package ca.recrown.islandsurvivalcraft.interaction.items;
import java.util.ArrayList;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.NamespacedKey;
import org.bukkit.block.Block;
import org.bukkit.block.BlockFace;
import org.bukkit.entity.HumanEntity;
import org.bukkit.entity.Player;
import org.bukkit.inventory.EquipmentSlot;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.Recipe;
import org.bukkit.inventory.ShapelessRecipe;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.inventory.meta.MapMeta;
import org.bukkit.map.MapCanvas;
import org.bukkit.map.MapRenderer;
import org.bukkit.map.MapView;
import ca.recrown.islandsurvivalcraft.IslandSurvivalCraft;
public class IslandMapItem extends MapRenderer implements VariedItem {
VariationItemIdentifier identifier;
NamespacedKey namespacedKey;
ShapelessRecipe recipe;
@Override
public void initialize(IslandSurvivalCraft islandSurvivalCraft) {
namespacedKey = new NamespacedKey(islandSurvivalCraft, "island_map");
ItemStack resultItem = new ItemStack(getBaseItem(), 1);
ItemMeta metadata = resultItem.getItemMeta();
metadata.setDisplayName(ChatColor.RESET + getName());
metadata.setLocalizedName(getName());
metadata.getPersistentDataContainer().set(namespacedKey, identifier, identifier.getID());
resultItem.setItemMeta(metadata);
recipe = new ShapelessRecipe(namespacedKey, resultItem);
recipe.addIngredient(Material.REDSTONE);
recipe.addIngredient(new ItemStack(Material.MAP));
}
@Override
public void render(MapView map, MapCanvas canvas, Player player) {
}
@Override
public Recipe getRecipe() {
return recipe;
}
@Override
public String getName() {
return ChatColor.GREEN + "Island Map";
}
@Override
public Material getBaseItem() {
return Material.MAP;
}
@Override
public NamespacedKey getNamespacedKey() {
return namespacedKey;
}
@Override
public void onItemCrafted(ItemStack item, HumanEntity crafter) {
ItemMeta metadata = item.getItemMeta();
ArrayList<String> lore = new ArrayList<>();
lore.add(ChatColor.RESET.toString() + ChatColor.GRAY + "Land ho!");
metadata.setLore(lore);
item.setItemMeta(metadata);
}
@Override
public void assignIdentifier(VariationItemIdentifier identifier) {
this.identifier = identifier;
}
@Override
public VariationItemIdentifier getIdentifier() {
return identifier;
}
@Override
public boolean needInitialization() {
return namespacedKey == null;
}
@Override
public boolean onItemRightClicked(ItemStack item, HumanEntity clicker, EquipmentSlot hand, Block block,
BlockFace blockFace) {
ItemStack freshMap = new ItemStack(Material.FILLED_MAP, 1);
MapMeta mapMeta = (MapMeta) freshMap.getItemMeta();
mapMeta.setDisplayName(ChatColor.RESET + getName());
MapView mapView = Bukkit.createMap(clicker.getWorld());
mapView.addRenderer(this);
mapView.setTrackingPosition(true);
mapMeta.setMapView(mapView);
freshMap.setItemMeta(mapMeta);
clicker.getInventory().setItem(hand, freshMap);
return false;
}
}

View File

@ -0,0 +1,19 @@
package ca.recrown.islandsurvivalcraft.interaction.items;
public enum RegisteredVariedItem {
ISLAND_MAP(new IslandMapItem())
;
private final VariedItem variedItem;
private RegisteredVariedItem(VariedItem alternateItem) {
this.variedItem = alternateItem;
this.variedItem.assignIdentifier(new VariationItemIdentifier(this.name()));
}
/**
* @return the variedItem
*/
public VariedItem getVariedItem() {
return variedItem;
}
}

View File

@ -0,0 +1,41 @@
package ca.recrown.islandsurvivalcraft.interaction.items;
import java.nio.charset.StandardCharsets;
import org.bukkit.persistence.PersistentDataAdapterContext;
import org.bukkit.persistence.PersistentDataType;
public class VariationItemIdentifier implements PersistentDataType<byte[], String> {
private final String ID;
public VariationItemIdentifier(String ID) {
this.ID = ID;
}
@Override
public Class<byte[]> getPrimitiveType() {
return byte[].class;
}
@Override
public Class<String> getComplexType() {
return String.class;
}
@Override
public byte [] toPrimitive(String complex, PersistentDataAdapterContext context) {
return complex.getBytes(StandardCharsets.UTF_8);
}
@Override
public String fromPrimitive(byte [] primitive, PersistentDataAdapterContext context) {
return new String(primitive, StandardCharsets.UTF_8);
}
/**
* @return the iD
*/
public String getID() {
return ID;
}
}

View File

@ -0,0 +1,35 @@
package ca.recrown.islandsurvivalcraft.interaction.items;
import org.bukkit.Material;
import org.bukkit.NamespacedKey;
import org.bukkit.block.Block;
import org.bukkit.block.BlockFace;
import org.bukkit.entity.HumanEntity;
import org.bukkit.inventory.EquipmentSlot;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.Recipe;
import ca.recrown.islandsurvivalcraft.IslandSurvivalCraft;
public interface VariedItem {
public void initialize(IslandSurvivalCraft islandSurvivalCraft);
public Recipe getRecipe();
public String getName();
public Material getBaseItem();
public NamespacedKey getNamespacedKey();
public void assignIdentifier(VariationItemIdentifier identifier);
public VariationItemIdentifier getIdentifier();
public boolean needInitialization();
public void onItemCrafted(ItemStack item, HumanEntity crafter);
/**
* Called when this item is right clicked with.
* @param item The itemstack that was right clicked with.
* @param clicker The player that did the clicking.
* @param hand The hand that it was in.
* @param block The block the item was right clicked on.
* @param blockFace The face of the block that was clicked.
* @return
*/
public boolean onItemRightClicked(ItemStack item, HumanEntity clicker, EquipmentSlot hand, Block block, BlockFace blockFace);
}

View File

@ -0,0 +1,79 @@
package ca.recrown.islandsurvivalcraft.interaction.items;
import org.bukkit.Bukkit;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.Event.Result;
import org.bukkit.event.block.Action;
import org.bukkit.event.inventory.CraftItemEvent;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import ca.recrown.islandsurvivalcraft.IslandSurvivalCraft;
public class VariedItemManager implements Listener {
private final IslandSurvivalCraft islandSurvivalCraft;
public VariedItemManager(IslandSurvivalCraft islandSurvivalCraft) {
this.islandSurvivalCraft = islandSurvivalCraft;
}
public void loadAllItems() {
RegisteredVariedItem[] items = RegisteredVariedItem.values();
for (int i = 0; i < items.length; i++) {
if (items[i].getVariedItem().needInitialization()) items[i].getVariedItem().initialize(islandSurvivalCraft);
islandSurvivalCraft.getServer().addRecipe(items[i].getVariedItem().getRecipe());
}
}
public void unloadAllItems() {
RegisteredVariedItem[] items = RegisteredVariedItem.values();
for (int i = 0; i < items.length; i++) {
Bukkit.removeRecipe(items[i].getVariedItem().getNamespacedKey());
}
}
@EventHandler(priority = EventPriority.MONITOR)
public void onItemCrafted(CraftItemEvent e) {
ItemStack item = e.getCurrentItem();
VariedItem variedItem = getVariedItemFromItemStack(item);
if (variedItem != null) {
variedItem.onItemCrafted(item, e.getWhoClicked());
}
}
@EventHandler(priority = EventPriority.HIGHEST)
public void onPlayerInteraction(PlayerInteractEvent e) {
if (e.useItemInHand() == Result.DENY || (e.getAction() != Action.RIGHT_CLICK_AIR && e.getAction() != Action.RIGHT_CLICK_BLOCK)) return;
ItemStack item = e.getItem();
VariedItem variedItem = getVariedItemFromItemStack(item);
if (variedItem != null) {
if(variedItem.onItemRightClicked(item, e.getPlayer(), e.getHand(), e.getClickedBlock(), e.getBlockFace())) {
e.setCancelled(true);
e.setUseItemInHand(Result.DENY);
}
}
}
private VariedItem getVariedItemFromItemStack(ItemStack item) {
if (item == null) return null;
ItemMeta meta = item.getItemMeta();
RegisteredVariedItem[] items = RegisteredVariedItem.values();
for (int i = 0; i < items.length; i++) {
VariedItem curr = items[i].getVariedItem();
String identifier = meta.getPersistentDataContainer().get(curr.getNamespacedKey(), curr.getIdentifier());
if (identifier != null) {
if (curr.getIdentifier().getID().equals(identifier)) {
return curr;
}
}
}
return null;
}
}

Binary file not shown.

View File

@ -1 +1 @@
Start-Process java -ArgumentList "-Xms512M", "-Xmx1G", "-jar", "paper-195.jar", "nogui"
Start-Process java -ArgumentList "-Xms512M", "-Xmx1G", "-jar", "paper.jar", "nogui"

View File

@ -1 +0,0 @@
Start-Process java -ArgumentList "-Xms512M", "-Xmx4G", "-jar", "paper-195.jar", "nogui"

View File

@ -1,5 +1,5 @@
write-Output "Attempting to start Paper test server."
$SID = Start-Process java -ArgumentList "-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=25566", "-Xms512M", "-Xmx1G", "-jar", "paper-195.jar", "nogui" -PassThru
$SID = Start-Process java -ArgumentList "-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=25566", "-Xms512M", "-Xmx1G", "-jar", "paper.jar", "nogui" -PassThru
$SID = $SID.Id
write-Output "Process started. PID is: $SID"