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.command.TabCompleter;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import net.kyori.adventure.text.Component;
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 +50,19 @@ 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()));
return true;
case "reset": case "reset":
return reset(); break;
case "skip": case "skip":
//TODO(Merchand): Skip trade //TODO(Merchand): Skip trade
break;
case "goto":
if(sender instanceof Player ply){
ply.teleport(m.getLocation());
}
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 +94,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

@ -4,33 +4,46 @@ 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.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.player.PlayerInteractEntityEvent;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.event.world.EntitiesUnloadEvent; 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.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 Material[] mat = Material.values();
private final Inventory inv;
private Villager m ;
public static Merchand getInstance(){ public static Merchand getInstance(){
if(instance != null) if(instance == null)
return instance; instance = new Merchand();
return new Merchand(); return instance;
} }
private Villager m ;
private Merchand() { private Merchand() {
generate(); inv = Bukkit.createInventory(null, 9);
} }
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));
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.setGlowing(true);
m.setInvulnerable(true); m.setInvulnerable(true);
Bukkit.getServer().forEachAudience((a) -> { Bukkit.getServer().forEachAudience((a) -> {
@ -38,16 +51,16 @@ 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.getLocation();
generate();
} }
@EventHandler public static Tuple<Material, Integer> genTrade(long hour){
public void onHour(HourEvent e){ Random rnd = new Random(hour);
regenerate(); return new Tuple<>(mat[rnd.nextInt(mat.length)], rnd.nextInt(9)+1);
} }
@EventHandler //TODO(Merchand): Check if can't do using ticket chunk @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{ 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

@ -4,6 +4,7 @@ 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 implements Listener{
@ -20,7 +21,7 @@ public class TheVoidRoad extends JavaPlugin implements Listener{
//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;
}
}