Compare commits

..

2 Commits

Author SHA1 Message Date
a7a775d1c8
Merchand materials + spawn management 2024-01-25 13:39:56 +01:00
94507d980d
Basics of Merchand 2024-01-23 22:08:02 +01:00
7 changed files with 428 additions and 48 deletions

View File

@ -12,10 +12,14 @@ import org.bukkit.command.CommandSender;
import org.bukkit.command.TabCompleter; import org.bukkit.command.TabCompleter;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import net.kyori.adventure.text.Component;
import ovh.herisson.thevoidroad.DatabaseManager;
import ovh.herisson.thevoidroad.TheVoidRoad; import ovh.herisson.thevoidroad.TheVoidRoad;
import ovh.herisson.thevoidroad.Voids; import ovh.herisson.thevoidroad.Voids;
import ovh.herisson.thevoidroad.Entity.Merchand;
public class VoidCommands implements CommandExecutor, TabCompleter{ public class VoidCommands implements CommandExecutor, TabCompleter{
private final Merchand m = Merchand.getInstance();
@Override @Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
@ -47,11 +51,23 @@ public class VoidCommands implements CommandExecutor, TabCompleter{
case "setspawn": case "setspawn":
return setspawn(sender); return setspawn(sender);
case "init": case "init":
return init(); m.regenerate();
Bukkit.getServer().sendMessage(Component.text(m.getLocation().toString()));
DatabaseManager.getInstance().setConfig("init", "true");
return true;
case "reset": case "reset":
return reset(); DatabaseManager.getInstance().setConfig("init", "false");
break;
case "skip": case "skip":
//TODO(Merchand): Skip trade //TODO(Merchand): Skip trade
break;
case "goto":
if(sender instanceof Player ply && m.getLocation() != null){
ply.teleport(m.getLocation());
}else{
sender.sendMessage("You can't teleport!");
}
break;
case "balance": case "balance":
if(args.length < 5) return false; if(args.length < 5) return false;
switch (args[2]) { switch (args[2]) {
@ -83,15 +99,6 @@ public class VoidCommands implements CommandExecutor, TabCompleter{
return false; return false;
} }
private boolean init(){
//TODO(merchand): init the game
return true;
}
private boolean reset(){
return false;
}
@Override @Override
public List<String> onTabComplete(CommandSender sender, Command command, String label, String[] args) { public List<String> onTabComplete(CommandSender sender, Command command, String label, String[] args) {
ArrayList<String> list = new ArrayList<>(); ArrayList<String> list = new ArrayList<>();

View File

@ -97,7 +97,8 @@ public class DatabaseManager {
st.close(); st.close();
} }
public String getConfig(String name) throws SQLException{ public String getConfig(String name){
try {
Statement st = con.createStatement(); Statement st = con.createStatement();
String query = "SELECT value FROM config WHERE name = ? LIMIT 1"; String query = "SELECT value FROM config WHERE name = ? LIMIT 1";
PreparedStatement p = con.prepareStatement(query); PreparedStatement p = con.prepareStatement(query);
@ -106,9 +107,14 @@ public class DatabaseManager {
String value = rs.getString(1); String value = rs.getString(1);
st.close(); st.close();
return value; return value;
} catch(Exception e){
e.printStackTrace();
}
return null;
} }
public void setConfig(String name, @Nullable String value) throws SQLException{ public void setConfig(String name, @Nullable String value){
try {
Statement st = con.createStatement(); Statement st = con.createStatement();
String query = "INSERT OR REPLACE INTO config (name, value) VALUES (?, ?)"; String query = "INSERT OR REPLACE INTO config (name, value) VALUES (?, ?)";
PreparedStatement p = con.prepareStatement(query); PreparedStatement p = con.prepareStatement(query);
@ -116,6 +122,9 @@ public class DatabaseManager {
p.setString(2, value); p.setString(2, value);
p.execute(); p.execute();
st.close(); st.close();
} catch(Exception e){
e.printStackTrace();
}
} }

View File

@ -1,36 +1,72 @@
package ovh.herisson.thevoidroad.Entity; package ovh.herisson.thevoidroad.Entity;
import java.io.File;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.Random; import java.util.Random;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.NamespacedKey;
import org.bukkit.entity.Entity;
import org.bukkit.entity.EntityType; import org.bukkit.entity.EntityType;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.entity.Villager; import org.bukkit.entity.Villager;
import org.bukkit.event.EventHandler; import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener; import org.bukkit.event.Listener;
import org.bukkit.event.world.EntitiesUnloadEvent; import org.bukkit.event.player.PlayerInteractEntityEvent;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.event.world.EntitiesLoadEvent;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import org.bukkit.persistence.PersistentDataType;
import net.kyori.adventure.text.Component;
import ovh.herisson.thevoidroad.DatabaseManager;
import ovh.herisson.thevoidroad.TheVoidRoad;
import ovh.herisson.thevoidroad.Event.HourEvent; import ovh.herisson.thevoidroad.Event.HourEvent;
import ovh.herisson.thevoidroad.util.Tuple;
//Unique, so singleton patern //Unique, so singleton patern
public class Merchand implements Listener{ public class Merchand implements Listener{
public static Tuple<Material, Integer> current, next;
private static Merchand instance; private static Merchand instance;
private static final ArrayList<Material> mat = new ArrayList<>();
private final Inventory inv;
private Villager m ;
private static final NamespacedKey tagger = new NamespacedKey(TheVoidRoad.instance, "merchand") ;
public static Merchand getInstance(){ public static Merchand getInstance(){
if(instance != null) if(instance == null)
instance = new Merchand();
return instance; return instance;
return new Merchand();
} }
private Villager m ;
private Merchand() { private Merchand() {
generate(); inv = Bukkit.createInventory(null, 9);
File f = new File(TheVoidRoad.instance.getDataFolder(), "materials.txt");
try {
for (String s : Files.readAllLines(f.toPath())) {
mat.add(Material.valueOf(s));
}
} catch(Exception e){
e.printStackTrace();
}
if(DatabaseManager.getInstance().getConfig("init").equalsIgnoreCase("true")){
regenerate();
}
} }
private void generate(){ public void regenerate(){
int x = new Random().nextInt(750 * 2) - 750, z = new Random().nextInt(750 * 2) - 750; if(m != null){ m.remove(); }
m = (Villager) Bukkit.getWorld("world").spawnEntity(new Location(Bukkit.getWorld("world"), x, 300, z), EntityType.VILLAGER); current = genTrade(System.currentTimeMillis() / (1000 * 60 * 60));
Location center = Bukkit.getWorld("world").getWorldBorder().getCenter();
double brdSize = Bukkit.getWorld("world").getWorldBorder().getSize();
double x = new Random().nextDouble(brdSize) - brdSize/2, z = new Random().nextDouble(brdSize) - brdSize/2;
m = (Villager) Bukkit.getWorld("world").spawnEntity(center.add(x, 100, z), EntityType.VILLAGER);
m.getPersistentDataContainer().set(tagger, PersistentDataType.BOOLEAN, true);
m.setGlowing(true); m.setGlowing(true);
m.setInvulnerable(true); m.setInvulnerable(true);
Bukkit.getServer().forEachAudience((a) -> { Bukkit.getServer().forEachAudience((a) -> {
@ -38,23 +74,51 @@ public class Merchand implements Listener{
ply.setCompassTarget(m.getLocation()); ply.setCompassTarget(m.getLocation());
} }
}); //Temporary }); //Temporary
Bukkit.getServer().sendMessage(Component.text("The merchand just spawned"));
} }
public void regenerate(){ public Location getLocation(){
if(m != null){ m.remove(); } return m != null ? m.getLocation(): null;
generate(); }
public static Tuple<Material, Integer> genTrade(long hour){
Random rnd = new Random(hour);
return new Tuple<>(mat.get(rnd.nextInt(mat.size())), rnd.nextInt(9)+1);
}
@EventHandler
public void checkTagged(EntitiesLoadEvent e){
for (Entity m : e.getEntities()) {
if(m.getPersistentDataContainer().has(tagger) && m.getPersistentDataContainer().get(tagger, PersistentDataType.BOOLEAN)){
if(!m.equals(this.m)){
m.remove();
}
}
}
} }
@EventHandler @EventHandler
public void onHour(HourEvent e){ public void onHour(HourEvent e){
current = genTrade(e.unixhour);
next = genTrade(e.unixhour + 1);
regenerate(); regenerate();
} }
@EventHandler //TODO(Merchand): Check if can't do using ticket chunk @EventHandler
public void onUnload(EntitiesUnloadEvent e){ public void OpenInventory(PlayerInteractEntityEvent e){
if(e.getEntities().contains(m)){ e.setCancelled(true);
m.getChunk().load(); inv.clear();
inv.setItem(4, new ItemStack(current.x, current.y));
if(e.getRightClicked().equals(m)){
e.getPlayer().sendMessage("Open inventory");
e.getPlayer().openInventory(inv);
} }
} }
@EventHandler
public void Compass(PlayerInteractEvent e){
if(m.getLocation() != null && e.getItem() != null && e.getItem().getType() == Material.COMPASS){
e.getPlayer().setCompassTarget(m.getLocation());
}
}
} }

View File

@ -13,6 +13,7 @@ import com.destroystokyo.paper.event.server.ServerTickStartEvent;
public class HourEvent extends Event implements Listener{ public class HourEvent extends Event implements Listener{
private static final HandlerList handlerList = new HandlerList(); private static final HandlerList handlerList = new HandlerList();
public int hour = LocalTime.now().getHour(); public int hour = LocalTime.now().getHour();
public long unixhour = System.currentTimeMillis() / (1000L * 60L * 60L);
public static HandlerList getHandlerList(){ public static HandlerList getHandlerList(){
return handlerList; return handlerList;
@ -27,6 +28,7 @@ public class HourEvent extends Event implements Listener{
public void onHour(ServerTickStartEvent e){ public void onHour(ServerTickStartEvent e){
if(LocalTime.now().getHour() != hour){ if(LocalTime.now().getHour() != hour){
hour = LocalTime.now().getHour(); hour = LocalTime.now().getHour();
unixhour = System.currentTimeMillis() / (1000L * 60L * 60L);
Bukkit.getPluginManager().callEvent(this); Bukkit.getPluginManager().callEvent(this);
} }
} }

View File

@ -1,26 +1,35 @@
package ovh.herisson.thevoidroad; package ovh.herisson.thevoidroad;
import org.bukkit.event.Listener;
import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.plugin.java.JavaPlugin;
import ovh.herisson.thevoidroad.Commands.VoidCommands; import ovh.herisson.thevoidroad.Commands.VoidCommands;
import ovh.herisson.thevoidroad.Entity.Merchand;
import ovh.herisson.thevoidroad.Event.HourEvent; import ovh.herisson.thevoidroad.Event.HourEvent;
public class TheVoidRoad extends JavaPlugin implements Listener{ public class TheVoidRoad extends JavaPlugin{
public static String CoinGlyph = ""; // Ɣ, , , , ṿ public static String CoinGlyph = ""; // Ɣ, , , , ṿ
public static final DatabaseManager db = DatabaseManager.getInstance(); public static final DatabaseManager db = DatabaseManager.getInstance();
public static TheVoidRoad instance;
public TheVoidRoad(){
super();
instance = this;
}
@Override @Override
public void onEnable(){ public void onEnable(){
saveResource("materials.txt", false);
//Commands //Commands
getCommand("void").setExecutor(new VoidCommands()); getCommand("void").setExecutor(new VoidCommands());
getCommand("void").setTabCompleter(new VoidCommands()); getCommand("void").setTabCompleter(new VoidCommands());
//Events //Events
getServer().getPluginManager().registerEvents(new HourEvent(), this); //For HourEvent getServer().getPluginManager().registerEvents(new HourEvent(), this); //For HourEvent
getServer().getPluginManager().registerEvents(this, this); getServer().getPluginManager().registerEvents(Merchand.getInstance(), this);
} }
@Override @Override

View File

@ -0,0 +1,10 @@
package ovh.herisson.thevoidroad.util;
public class Tuple<X, Y> {
public final X x;
public final Y y;
public Tuple(X x, Y y) {
this.x = x;
this.y = y;
}
}

View File

@ -0,0 +1,279 @@
STONE
GRANITE
POLISHED_GRANITE
DIORITE
POLISHED_DIORITE
ANDESITE
POLISHED_ANDESITE
DEEPSLATE
COBBLED_DEEPSLATE
POLISHED_DEEPSLATE
CALCITE
TUFF
TUFF_WALL
CHISELED_TUFF
POLISHED_TUFF
POLISHED_TUFF_WALL
TUFF_BRICKS
TUFF_BRICK_WALL
CHISELED_TUFF_BRICKS
DRIPSTONE_BLOCK
GRASS_BLOCK
DIRT
COARSE_DIRT
PODZOL
ROOTED_DIRT
MUD
COBBLESTONE
OAK_PLANKS
SPRUCE_PLANKS
BIRCH_PLANKS
JUNGLE_PLANKS
ACACIA_PLANKS
CHERRY_PLANKS
DARK_OAK_PLANKS
MANGROVE_PLANKS
BAMBOO_PLANKS
BAMBOO_MOSAIC
OAK_SAPLING
SPRUCE_SAPLING
BIRCH_SAPLING
JUNGLE_SAPLING
ACACIA_SAPLING
CHERRY_SAPLING
DARK_OAK_SAPLING
MANGROVE_PROPAGULE
SAND
RED_SAND
GRAVEL
OAK_LOG
SPRUCE_LOG
BIRCH_LOG
JUNGLE_LOG
ACACIA_LOG
CHERRY_LOG
DARK_OAK_LOG
MANGROVE_LOG
MANGROVE_ROOTS
MUDDY_MANGROVE_ROOTS
BAMBOO_BLOCK
OAK_WOOD
SPRUCE_WOOD
BIRCH_WOOD
JUNGLE_WOOD
ACACIA_WOOD
CHERRY_WOOD
DARK_OAK_WOOD
MANGROVE_WOOD
OAK_LEAVES
SPRUCE_LEAVES
BIRCH_LEAVES
JUNGLE_LEAVES
ACACIA_LEAVES
CHERRY_LEAVES
DARK_OAK_LEAVES
MANGROVE_LEAVES
AZALEA_LEAVES
FLOWERING_AZALEA_LEAVES
GLASS
SANDSTONE
SHORT_GRASS
AZALEA
FLOWERING_AZALEA
SEA_PICKLE
WHITE_WOOL
BROWN_MUSHROOM
RED_MUSHROOM
SUGAR_CANE
KELP
BAMBOO
BRICKS
BOOKSHELF
MOSSY_COBBLESTONE
OBSIDIAN
TORCH
CHEST
CRAFTING_TABLE
FURNACE
LADDER
SNOW
ICE
SNOW_BLOCK
CACTUS
CLAY
JUKEBOX
PUMPKIN
STONE_BRICKS
PACKED_MUD
IRON_BARS
CHAIN
MELON
LILY_PAD
ENCHANTING_TABLE
COBBLESTONE_WALL
ANVIL
HAY_BLOCK
TERRACOTTA
PACKED_ICE
RED_SANDSTONE
MAGMA_BLOCK
BONE_BLOCK
WHITE_CONCRETE
ORANGE_CONCRETE
MAGENTA_CONCRETE
LIGHT_BLUE_CONCRETE
YELLOW_CONCRETE
LIME_CONCRETE
PINK_CONCRETE
GRAY_CONCRETE
LIGHT_GRAY_CONCRETE
CYAN_CONCRETE
PURPLE_CONCRETE
BLUE_CONCRETE
BROWN_CONCRETE
GREEN_CONCRETE
RED_CONCRETE
BLACK_CONCRETE
TURTLE_EGG
BLUE_ICE
SCAFFOLDING
REDSTONE
REDSTONE_TORCH
REDSTONE_BLOCK
REPEATER
COMPARATOR
PISTON
STICKY_PISTON
SLIME_BLOCK
HONEY_BLOCK
OBSERVER
HOPPER
DISPENSER
DROPPER
LECTERN
TARGET
LEVER
LIGHTNING_ROD
DAYLIGHT_DETECTOR
TRIPWIRE_HOOK
TRAPPED_CHEST
TNT
REDSTONE_LAMP
NOTE_BLOCK
STONE_BUTTON
STONE_PRESSURE_PLATE
LIGHT_WEIGHTED_PRESSURE_PLATE
HEAVY_WEIGHTED_PRESSURE_PLATE
OAK_PRESSURE_PLATE
SPRUCE_PRESSURE_PLATE
BIRCH_PRESSURE_PLATE
JUNGLE_PRESSURE_PLATE
ACACIA_PRESSURE_PLATE
CHERRY_PRESSURE_PLATE
DARK_OAK_PRESSURE_PLATE
MANGROVE_PRESSURE_PLATE
BAMBOO_PRESSURE_PLATE
RAIL
SADDLE
MINECART
SCUTE
FLINT_AND_STEEL
APPLE
ARROW
COAL
CHARCOAL
DIAMOND
EMERALD
LAPIS_LAZULI
AMETHYST_SHARD
RAW_IRON
IRON_INGOT
RAW_COPPER
COPPER_INGOT
RAW_GOLD
GOLD_INGOT
STICK
BOWL
MUSHROOM_STEW
STRING
FEATHER
GUNPOWDER
WHEAT_SEEDS
WHEAT
BREAD
FLINT
PORKCHOP
PAINTING
GOLDEN_APPLE
BUCKET
WATER_BUCKET
LAVA_BUCKET
POWDER_SNOW_BUCKET
SNOWBALL
LEATHER
MILK_BUCKET
BRICK
CLAY_BALL
DRIED_KELP_BLOCK
PAPER
BOOK
SLIME_BALL
EGG
COD
SALMON
TROPICAL_FISH
PUFFERFISH
INK_SAC
COCOA_BEANS
WHITE_DYE
ORANGE_DYE
MAGENTA_DYE
LIGHT_BLUE_DYE
YELLOW_DYE
LIME_DYE
PINK_DYE
GRAY_DYE
LIGHT_GRAY_DYE
CYAN_DYE
PURPLE_DYE
BLUE_DYE
BROWN_DYE
GREEN_DYE
RED_DYE
BLACK_DYE
BONE_MEAL
BONE
SUGAR
COOKIE
FILLED_MAP
MELON_SLICE
DRIED_KELP
PUMPKIN_SEEDS
MELON_SEEDS
BEEF
CHICKEN
ROTTEN_FLESH
GOLD_NUGGET
GLASS_BOTTLE
SPIDER_EYE
CARROT
POTATO
POISONOUS_POTATO
RABBIT
RABBIT_FOOT
RABBIT_HIDE
LEAD
MUTTON
BEETROOT
TOTEM_OF_UNDYING
IRON_NUGGET
PHANTOM_MEMBRANE
NAUTILUS_SHELL
GOAT_HORN
SWEET_BERRIES
GLOW_BERRIES
HONEYCOMB
BEE_NEST
HONEY_BOTTLE
HONEYCOMB_BLOCK
CANDLE