|
|
package com.zivilon.cinder_loe.command;
|
|
|
|
|
|
import com.zivilon.cinder_loe.carriage.CarriageDestination;
|
|
|
import com.zivilon.cinder_loe.carriage.CarriageDestinationsData;
|
|
|
import com.zivilon.cinder_loe.carriage.CarriageRoadRegistry;
|
|
|
import com.zivilon.cinder_loe.network.PacketOpenCarriageGui;
|
|
|
import lotr.common.network.LOTRPacketHandler;
|
|
|
import net.minecraft.command.CommandBase;
|
|
|
import net.minecraft.command.CommandException;
|
|
|
import net.minecraft.command.ICommandSender;
|
|
|
|
|
|
import net.minecraft.entity.player.EntityPlayerMP;
|
|
|
import net.minecraft.world.World;
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
import java.util.List;
|
|
|
|
|
|
public class CommandOpenCarriageMenu extends CommandBase {
|
|
|
@Override
|
|
|
public String getCommandName() {
|
|
|
return "open_carriage_menu";
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public String getCommandUsage(ICommandSender sender) {
|
|
|
return "/open_carriage_menu";
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public void processCommand(ICommandSender sender, String[] args) throws CommandException {
|
|
|
if (!(sender instanceof EntityPlayerMP)) {
|
|
|
throw new CommandException("Must be run by a player.");
|
|
|
}
|
|
|
EntityPlayerMP player = (EntityPlayerMP) sender;
|
|
|
World world = player.worldObj;
|
|
|
|
|
|
CarriageDestinationsData data = CarriageDestinationsData.get(world);
|
|
|
if (data == null) {
|
|
|
throw new CommandException("Carriage data unavailable.");
|
|
|
}
|
|
|
|
|
|
// 1) first get the absolute nearest stops, ignoring roads
|
|
|
List<CarriageDestination> rawNearby = data.getNearestFour(player.posX, player.posZ, "", CarriageRoadRegistry.INSTANCE);
|
|
|
if (rawNearby.isEmpty()) {
|
|
|
throw new CommandException("No nearby carriage stops found.");
|
|
|
}
|
|
|
|
|
|
// 2) grab the road of the very nearest
|
|
|
String startRoad = rawNearby.get(0).road;
|
|
|
|
|
|
// 3) now get up to 4 stops on that road and its neighbors
|
|
|
List<CarriageDestination> nearest = data.getNearestFour(
|
|
|
player.posX, player.posZ,
|
|
|
startRoad,
|
|
|
/* pass your registry singleton, not `new` */ CarriageRoadRegistry.INSTANCE
|
|
|
);
|
|
|
// if you don’t have an instance() method, just call the static methods inside getNearestFour
|
|
|
|
|
|
if (nearest.isEmpty()) {
|
|
|
throw new CommandException("No stops on your road or its neighbors.");
|
|
|
}
|
|
|
|
|
|
// 4) build your packet
|
|
|
List<String> names = new ArrayList<>();
|
|
|
List<Integer> costs = new ArrayList<>();
|
|
|
List<String> roads = new ArrayList<>();
|
|
|
|
|
|
for (CarriageDestination d : nearest) {
|
|
|
double dx = d.x - player.posX;
|
|
|
double dz = d.z - player.posZ;
|
|
|
int cost = Math.max(1, (int)(Math.hypot(dx, dz) / 50));
|
|
|
names .add(d.name);
|
|
|
costs .add(cost);
|
|
|
roads .add(d.road);
|
|
|
}
|
|
|
|
|
|
LOTRPacketHandler.networkWrapper
|
|
|
.sendTo(new PacketOpenCarriageGui(names, costs, roads), player);
|
|
|
}
|
|
|
}
|