Merchand materials + spawn management
This commit is contained in:
parent
94507d980d
commit
a7a775d1c8
@ -13,6 +13,7 @@ import org.bukkit.command.TabCompleter;
|
||||
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.Voids;
|
||||
import ovh.herisson.thevoidroad.Entity.Merchand;
|
||||
@ -52,15 +53,19 @@ public class VoidCommands implements CommandExecutor, TabCompleter{
|
||||
case "init":
|
||||
m.regenerate();
|
||||
Bukkit.getServer().sendMessage(Component.text(m.getLocation().toString()));
|
||||
DatabaseManager.getInstance().setConfig("init", "true");
|
||||
return true;
|
||||
case "reset":
|
||||
DatabaseManager.getInstance().setConfig("init", "false");
|
||||
break;
|
||||
case "skip":
|
||||
//TODO(Merchand): Skip trade
|
||||
break;
|
||||
case "goto":
|
||||
if(sender instanceof Player ply){
|
||||
if(sender instanceof Player ply && m.getLocation() != null){
|
||||
ply.teleport(m.getLocation());
|
||||
}else{
|
||||
sender.sendMessage("You can't teleport!");
|
||||
}
|
||||
break;
|
||||
case "balance":
|
||||
|
@ -97,25 +97,34 @@ public class DatabaseManager {
|
||||
st.close();
|
||||
}
|
||||
|
||||
public String getConfig(String name) throws SQLException{
|
||||
Statement st = con.createStatement();
|
||||
String query = "SELECT value FROM config WHERE name = ? LIMIT 1";
|
||||
PreparedStatement p = con.prepareStatement(query);
|
||||
p.setString(1, name);
|
||||
ResultSet rs = p.executeQuery();
|
||||
String value = rs.getString(1);
|
||||
st.close();
|
||||
return value;
|
||||
public String getConfig(String name){
|
||||
try {
|
||||
Statement st = con.createStatement();
|
||||
String query = "SELECT value FROM config WHERE name = ? LIMIT 1";
|
||||
PreparedStatement p = con.prepareStatement(query);
|
||||
p.setString(1, name);
|
||||
ResultSet rs = p.executeQuery();
|
||||
String value = rs.getString(1);
|
||||
st.close();
|
||||
return value;
|
||||
} catch(Exception e){
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public void setConfig(String name, @Nullable String value) throws SQLException{
|
||||
Statement st = con.createStatement();
|
||||
String query = "INSERT OR REPLACE INTO config (name, value) VALUES (?, ?)";
|
||||
PreparedStatement p = con.prepareStatement(query);
|
||||
p.setString(1, name);
|
||||
p.setString(2, value);
|
||||
p.execute();
|
||||
st.close();
|
||||
public void setConfig(String name, @Nullable String value){
|
||||
try {
|
||||
Statement st = con.createStatement();
|
||||
String query = "INSERT OR REPLACE INTO config (name, value) VALUES (?, ?)";
|
||||
PreparedStatement p = con.prepareStatement(query);
|
||||
p.setString(1, name);
|
||||
p.setString(2, value);
|
||||
p.execute();
|
||||
st.close();
|
||||
} catch(Exception e){
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
@ -1,10 +1,15 @@
|
||||
package ovh.herisson.thevoidroad.Entity;
|
||||
|
||||
import java.io.File;
|
||||
import java.nio.file.Files;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Random;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
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.Player;
|
||||
import org.bukkit.entity.Villager;
|
||||
@ -12,11 +17,14 @@ import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.Listener;
|
||||
import org.bukkit.event.player.PlayerInteractEntityEvent;
|
||||
import org.bukkit.event.player.PlayerInteractEvent;
|
||||
import org.bukkit.event.world.EntitiesUnloadEvent;
|
||||
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.util.Tuple;
|
||||
|
||||
@ -24,10 +32,12 @@ import ovh.herisson.thevoidroad.util.Tuple;
|
||||
public class Merchand implements Listener{
|
||||
public static Tuple<Material, Integer> current, next;
|
||||
private static Merchand instance;
|
||||
private static final Material[] mat = Material.values();
|
||||
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(){
|
||||
if(instance == null)
|
||||
instance = new Merchand();
|
||||
@ -36,14 +46,27 @@ public class Merchand implements Listener{
|
||||
|
||||
private Merchand() {
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
||||
public void regenerate(){
|
||||
if(m != null){ m.remove(); }
|
||||
current = genTrade(System.currentTimeMillis() / (1000 * 60 * 60));
|
||||
int x = new Random().nextInt(750) - 750/2, z = new Random().nextInt(750) - 750/2;
|
||||
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.setInvulnerable(true);
|
||||
Bukkit.getServer().forEachAudience((a) -> {
|
||||
@ -55,18 +78,22 @@ public class Merchand implements Listener{
|
||||
}
|
||||
|
||||
public Location getLocation(){
|
||||
return m.getLocation();
|
||||
return m != null ? m.getLocation(): null;
|
||||
}
|
||||
|
||||
public static Tuple<Material, Integer> genTrade(long hour){
|
||||
Random rnd = new Random(hour);
|
||||
return new Tuple<>(mat[rnd.nextInt(mat.length)], rnd.nextInt(9)+1);
|
||||
return new Tuple<>(mat.get(rnd.nextInt(mat.size())), rnd.nextInt(9)+1);
|
||||
}
|
||||
|
||||
@EventHandler //TODO(Merchand): Check if can't do using ticket chunk
|
||||
public void onUnload(EntitiesUnloadEvent e){
|
||||
if(e.getEntities().contains(m)){
|
||||
m.getChunk().load();
|
||||
@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();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -90,7 +117,7 @@ public class Merchand implements Listener{
|
||||
|
||||
@EventHandler
|
||||
public void Compass(PlayerInteractEvent e){
|
||||
if(e.getItem() != null && e.getItem().getType() == Material.COMPASS){
|
||||
if(m.getLocation() != null && e.getItem() != null && e.getItem().getType() == Material.COMPASS){
|
||||
e.getPlayer().setCompassTarget(m.getLocation());
|
||||
}
|
||||
}
|
||||
|
@ -1,20 +1,27 @@
|
||||
package ovh.herisson.thevoidroad;
|
||||
|
||||
import org.bukkit.event.Listener;
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
|
||||
import ovh.herisson.thevoidroad.Commands.VoidCommands;
|
||||
import ovh.herisson.thevoidroad.Entity.Merchand;
|
||||
import ovh.herisson.thevoidroad.Event.HourEvent;
|
||||
|
||||
public class TheVoidRoad extends JavaPlugin implements Listener{
|
||||
public class TheVoidRoad extends JavaPlugin{
|
||||
|
||||
public static String CoinGlyph = "Ⓥ"; // Ɣ, √, ▼, Ṿ, ṿ
|
||||
public static final DatabaseManager db = DatabaseManager.getInstance();
|
||||
public static TheVoidRoad instance;
|
||||
|
||||
public TheVoidRoad(){
|
||||
super();
|
||||
instance = this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onEnable(){
|
||||
|
||||
saveResource("materials.txt", false);
|
||||
|
||||
//Commands
|
||||
getCommand("void").setExecutor(new VoidCommands());
|
||||
getCommand("void").setTabCompleter(new VoidCommands());
|
||||
@ -22,6 +29,7 @@ public class TheVoidRoad extends JavaPlugin implements Listener{
|
||||
//Events
|
||||
getServer().getPluginManager().registerEvents(new HourEvent(), this); //For HourEvent
|
||||
getServer().getPluginManager().registerEvents(Merchand.getInstance(), this);
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
|
279
TheVoidRoad/src/main/resources/materials.txt
Normal file
279
TheVoidRoad/src/main/resources/materials.txt
Normal 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
|
Loading…
Reference in New Issue
Block a user