Basics of Merchand

This commit is contained in:
Debucquoy Anthony 2024-01-23 22:08:02 +01:00
parent 42c25d7095
commit 94507d980d
Signed by: tonitch
GPG Key ID: A78D6421F083D42E
5 changed files with 79 additions and 27 deletions

View File

@ -12,10 +12,13 @@ import org.bukkit.command.CommandSender;
import org.bukkit.command.TabCompleter;
import org.bukkit.entity.Player;
import net.kyori.adventure.text.Component;
import ovh.herisson.thevoidroad.TheVoidRoad;
import ovh.herisson.thevoidroad.Voids;
import ovh.herisson.thevoidroad.Entity.Merchand;
public class VoidCommands implements CommandExecutor, TabCompleter{
private final Merchand m = Merchand.getInstance();
@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
@ -47,11 +50,19 @@ public class VoidCommands implements CommandExecutor, TabCompleter{
case "setspawn":
return setspawn(sender);
case "init":
return init();
m.regenerate();
Bukkit.getServer().sendMessage(Component.text(m.getLocation().toString()));
return true;
case "reset":
return reset();
break;
case "skip":
//TODO(Merchand): Skip trade
break;
case "goto":
if(sender instanceof Player ply){
ply.teleport(m.getLocation());
}
break;
case "balance":
if(args.length < 5) return false;
switch (args[2]) {
@ -74,7 +85,7 @@ public class VoidCommands implements CommandExecutor, TabCompleter{
if(sender instanceof Player ply){
Location loc = ply.getLocation();
WorldBorder border = ply.getWorld().getWorldBorder();
ply.getWorld().setSpawnLocation(loc);
border.setCenter(loc);
border.setSize(750);
@ -83,15 +94,6 @@ public class VoidCommands implements CommandExecutor, TabCompleter{
return false;
}
private boolean init(){
//TODO(merchand): init the game
return true;
}
private boolean reset(){
return false;
}
@Override
public List<String> onTabComplete(CommandSender sender, Command command, String label, String[] args) {
ArrayList<String> list = new ArrayList<>();

View File

@ -4,33 +4,46 @@ import java.util.Random;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.Player;
import org.bukkit.entity.Villager;
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.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import net.kyori.adventure.text.Component;
import ovh.herisson.thevoidroad.Event.HourEvent;
import ovh.herisson.thevoidroad.util.Tuple;
//Unique, so singleton patern
public class Merchand implements Listener{
public static Tuple<Material, Integer> current, next;
private static Merchand instance;
private static final Material[] mat = Material.values();
private final Inventory inv;
private Villager m ;
public static Merchand getInstance(){
if(instance != null)
return instance;
return new Merchand();
if(instance == null)
instance = new Merchand();
return instance;
}
private Villager m ;
private Merchand() {
generate();
inv = Bukkit.createInventory(null, 9);
}
private void generate(){
int x = new Random().nextInt(750 * 2) - 750, z = new Random().nextInt(750 * 2) - 750;
m = (Villager) Bukkit.getWorld("world").spawnEntity(new Location(Bukkit.getWorld("world"), x, 300, z), EntityType.VILLAGER);
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();
m = (Villager) Bukkit.getWorld("world").spawnEntity(center.add(x, 100, z), EntityType.VILLAGER);
m.setGlowing(true);
m.setInvulnerable(true);
Bukkit.getServer().forEachAudience((a) -> {
@ -38,16 +51,16 @@ public class Merchand implements Listener{
ply.setCompassTarget(m.getLocation());
}
}); //Temporary
Bukkit.getServer().sendMessage(Component.text("The merchand just spawned"));
}
public void regenerate(){
if(m != null){ m.remove(); }
generate();
public Location getLocation(){
return m.getLocation();
}
@EventHandler
public void onHour(HourEvent e){
regenerate();
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);
}
@EventHandler //TODO(Merchand): Check if can't do using ticket chunk
@ -57,4 +70,28 @@ public class Merchand implements Listener{
}
}
@EventHandler
public void onHour(HourEvent e){
current = genTrade(e.unixhour);
next = genTrade(e.unixhour + 1);
regenerate();
}
@EventHandler
public void OpenInventory(PlayerInteractEntityEvent e){
e.setCancelled(true);
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(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{
private static final HandlerList handlerList = new HandlerList();
public int hour = LocalTime.now().getHour();
public long unixhour = System.currentTimeMillis() / (1000L * 60L * 60L);
public static HandlerList getHandlerList(){
return handlerList;
@ -27,6 +28,7 @@ public class HourEvent extends Event implements Listener{
public void onHour(ServerTickStartEvent e){
if(LocalTime.now().getHour() != hour){
hour = LocalTime.now().getHour();
unixhour = System.currentTimeMillis() / (1000L * 60L * 60L);
Bukkit.getPluginManager().callEvent(this);
}
}

View File

@ -4,6 +4,7 @@ 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{
@ -20,7 +21,7 @@ public class TheVoidRoad extends JavaPlugin implements Listener{
//Events
getServer().getPluginManager().registerEvents(new HourEvent(), this); //For HourEvent
getServer().getPluginManager().registerEvents(this, this);
getServer().getPluginManager().registerEvents(Merchand.getInstance(), this);
}
@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;
}
}