Skip to content

Commit

Permalink
Refactors Cars to inherit CarDetails, which are setup in the config.yml
Browse files Browse the repository at this point in the history
Car Types are unique names which have an associated CarDetails (standard, racecar, etc.)
Conversation to allow for creation of CarType
Fixes and update dependencies
  • Loading branch information
A5H73Y committed Jan 22, 2020
1 parent 3ce45a6 commit 1c1c78e
Show file tree
Hide file tree
Showing 15 changed files with 387 additions and 147 deletions.
8 changes: 4 additions & 4 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,14 @@
<dependency>
<groupId>org.spigotmc</groupId>
<artifactId>spigot-api</artifactId>
<version>1.14.2-R0.1-SNAPSHOT</version>
<version>1.15.2-R0.1-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
<!--Bukkit API-->
<dependency>
<groupId>org.bukkit</groupId>
<artifactId>bukkit</artifactId>
<version>1.14.2-R0.1-SNAPSHOT</version>
<version>1.15.2-R0.1-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
<!--Vault API-->
Expand All @@ -81,7 +81,7 @@
<dependency>
<groupId>org.bstats</groupId>
<artifactId>bstats-bukkit</artifactId>
<version>1.4</version>
<version>1.7</version>
<scope>compile</scope>
</dependency>
<!-- Updater -->
Expand Down Expand Up @@ -121,7 +121,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.1.1</version>
<version>3.2.0</version>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/io/github/a5h73y/Carz.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public void onEnable() {
setupPlugins();

getLogger().info("Enabled Carz v" + getDescription().getVersion());
new Metrics(this);
new Metrics(this, 42269);
updatePlugin();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.github.a5h73y.commands;

import io.github.a5h73y.Carz;
import io.github.a5h73y.conversation.CreateCarType;
import io.github.a5h73y.enums.Commands;
import io.github.a5h73y.other.Utils;
import io.github.a5h73y.utility.TranslationUtils;
Expand All @@ -9,6 +10,7 @@
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.command.ConsoleCommandSender;
import org.bukkit.entity.Player;

/**
Expand All @@ -24,7 +26,7 @@ public CarzConsoleCommands(Carz carz) {

@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
if (sender instanceof Player) {
if (sender instanceof Player || !(sender instanceof ConsoleCommandSender)) {
sender.sendMessage(Carz.getPrefix() + "Use /carz instead.");
return false;
}
Expand Down Expand Up @@ -72,6 +74,10 @@ public boolean onCommand(CommandSender sender, Command command, String label, St
TranslationUtils.sendTranslation("Carz.ConfigReloaded", sender);
break;

case "createtype":
new CreateCarType((ConsoleCommandSender) sender).begin();
break;

case "cmds":
sender.sendMessage("/carzc spawn (player)");
sender.sendMessage("/carzc addCB");
Expand Down
14 changes: 10 additions & 4 deletions src/main/java/io/github/a5h73y/configuration/Settings.java
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ private void setupConfig() {
carz.getConfig().addDefault("Other.DestroyInLiquid", true);
carz.getConfig().addDefault("Other.UsePermissions", true);
carz.getConfig().addDefault("Other.UseEffects", true);
carz.getConfig().addDefault("Other.UseAutoTabCompletion", true);
carz.getConfig().addDefault("Other.UpdateCheck", true);

carz.getConfig().addDefault("Other.BountifulAPI.Enabled", true);
Expand All @@ -82,6 +83,11 @@ private void setupConfig() {
carz.getConfig().addDefault("Other.Vault.Cost.Upgrade", 8.0);
carz.getConfig().addDefault("Other.Vault.Cost.Refuel", 2.0);

carz.getConfig().addDefault("CarTypes.Default.StartMaxSpeed", 1.0);
carz.getConfig().addDefault("CarTypes.Default.Acceleration", 1.0);
carz.getConfig().addDefault("CarTypes.Default.FuelUsage", 1.0);
carz.getConfig().addDefault("CarTypes.Default.FillMaterial", "AIR");

carz.getConfig().options().copyDefaults(true);
carz.saveConfig();
}
Expand Down Expand Up @@ -179,19 +185,19 @@ public boolean isFuelScaleCost() {
return carz.getConfig().getBoolean("Fuel.ScaleCost");
}

public Double getStartSpeed() {
public double getStartSpeed() {
return carz.getConfig().getDouble("Speed.Start");
}

public Double getUpgradeSpeed() {
public double getUpgradeIncrement() {
return carz.getConfig().getDouble("Speed.Upgrade.Increment");
}

public Double getUpgradeMaxSpeed() {
public double getUpgradeMaxSpeed() {
return carz.getConfig().getDouble("Speed.Upgrade.Max");
}

public Double getClimbBlockStrength() {
public double getClimbBlockStrength() {
return carz.getConfig().getDouble("ClimbBlocks.Strength");
}

Expand Down
45 changes: 34 additions & 11 deletions src/main/java/io/github/a5h73y/controllers/CarController.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import io.github.a5h73y.Carz;
import io.github.a5h73y.enums.Permissions;
import io.github.a5h73y.model.Car;
import io.github.a5h73y.model.StandardCar;
import io.github.a5h73y.model.CarDetails;
import io.github.a5h73y.other.Utils;
import io.github.a5h73y.utility.EffectUtils;
import io.github.a5h73y.utility.PermissionUtils;
Expand All @@ -21,6 +22,8 @@
*/
public class CarController {

public static final String DEFAULT_CAR = "Default";

private final Carz carz;

// Vehicle ID with it's associated Car
Expand All @@ -29,8 +32,24 @@ public class CarController {
// Players currently inside a Carz vehicle, with the value being Vehicle ID
private final Map<String, Integer> playersDriving = new HashMap<>();

private final Map<String, CarDetails> carTypes = new HashMap<>();

public CarController(Carz carz) {
this.carz = carz;
populateCarTypes();
}

private void populateCarTypes() {
Set<String> allCarTypes = carz.getConfig().getConfigurationSection("CarTypes").getKeys(false);

for (String carType : allCarTypes) {
double startSpeed = carz.getConfig().getDouble("CarTypes." + carType + ".StartMaxSpeed");
double maxSpeed = carz.getConfig().getDouble("CarTypes." + carType + ".MaxUpgradeSpeed");
double acceleration = carz.getConfig().getDouble("CarTypes." + carType + ".Acceleration");
double fuelUsage = carz.getConfig().getDouble("CarTypes." + carType + ".FuelUsage");
String fillMaterial = carz.getConfig().getString("CarTypes." + carType + ".FillMaterial");
carTypes.put(carType, new CarDetails(startSpeed, maxSpeed, acceleration, fuelUsage, fillMaterial));
}
}

/**
Expand All @@ -40,7 +59,7 @@ public CarController(Carz carz) {
* @param owner is the player the registered owner?
*/
public void startDriving(String playerName, Integer carId, boolean owner) {
Car car = getOrCreateCar(carId, new StandardCar(carId));
Car car = getOrCreateCar(carId, DEFAULT_CAR);
if (owner) {
car.setOwner(playerName);
}
Expand All @@ -58,8 +77,8 @@ public void startDriving(String playerName, Integer carId) {
* @param carType
* @return matching or new Car
*/
public Car getOrCreateCar(Integer entityId, Car carType) {
return entityIdToCar.computeIfAbsent(entityId, k -> carType);
public Car getOrCreateCar(Integer entityId, String carType) {
return entityIdToCar.computeIfAbsent(entityId, k -> new Car(entityId, carType));
}

/**
Expand All @@ -68,7 +87,7 @@ public Car getOrCreateCar(Integer entityId, Car carType) {
* @param entityId
*/
public Car getOrCreateCar(Integer entityId) {
return getOrCreateCar(entityId, new StandardCar(entityId));
return getOrCreateCar(entityId, DEFAULT_CAR);
}

/**
Expand Down Expand Up @@ -168,7 +187,7 @@ public void upgradeCarSpeed(Player player) {
upgradeCarSpeed(car);
EffectUtils.playEffect(player, Effect.ZOMBIE_CHEW_WOODEN_DOOR);
player.sendMessage(TranslationUtils.getTranslation("Car.UpgradeSpeed")
.replace("%SPEED%", car.getMaxSpeed().toString()));
.replace("%SPEED%", String.valueOf(car.getMaxSpeed())));

EffectUtils.createUpgradeEffect((Vehicle) player.getVehicle());
}
Expand All @@ -180,14 +199,18 @@ public void upgradeCarSpeed(Player player) {
* @param car
*/
private void upgradeCarSpeed(Car car) {
Double currentSpeed = car.getMaxSpeed();
Double upgradeBy = carz.getSettings().getUpgradeSpeed();
Double maxSpeed = carz.getSettings().getUpgradeMaxSpeed();
double currentMax = car.getCarDetails().getStartMaxSpeed();
double maxSpeed = car.getCarDetails().getMaxUpgradeSpeed();
double upgradeBy = carz.getSettings().getUpgradeIncrement();

if ((currentSpeed + upgradeBy) > maxSpeed) {//&& !event.getPlayer().hasPermission("Carz.Admin"))
if ((currentMax + upgradeBy) > maxSpeed) {//&& !event.getPlayer().hasPermission("Carz.Admin"))
return;
}

car.setMaxSpeed(currentSpeed + upgradeBy);
car.setMaxSpeed(currentMax + upgradeBy);
}

public Map<String, CarDetails> getCarTypes() {
return carTypes;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public void displayFuelLevel(Player player) {
}

public void refuel(Car car) {
car.setFuel(MAX_FUEL);
car.setCurrentFuel(MAX_FUEL);
}

public void refuel(Car car, Player player) {
Expand All @@ -75,7 +75,7 @@ public void refuel(Car car, Player player) {
*/
private String formattedFuelLevel(Car car) {
StringBuilder sb = new StringBuilder();
double fuelRemaining = Math.floor((car.getFuel() / MAX_FUEL) * GAUGE_SCALE);
double fuelRemaining = Math.floor((car.getCurrentFuel() / MAX_FUEL) * GAUGE_SCALE);
double fuelMissing = GAUGE_SCALE - fuelRemaining;

sb.append(ChatColor.RED);
Expand Down
47 changes: 47 additions & 0 deletions src/main/java/io/github/a5h73y/conversation/CarzConversation.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package io.github.a5h73y.conversation;

import io.github.a5h73y.Carz;
import org.bukkit.ChatColor;
import org.bukkit.conversations.Conversable;
import org.bukkit.conversations.Conversation;
import org.bukkit.conversations.ConversationAbandonedEvent;
import org.bukkit.conversations.ConversationAbandonedListener;
import org.bukkit.conversations.ConversationContext;
import org.bukkit.conversations.ConversationFactory;
import org.bukkit.conversations.Prompt;

public abstract class CarzConversation implements ConversationAbandonedListener {

private ConversationFactory conversationFactory;
private Conversable player;

public abstract Prompt getEntryPrompt();

public CarzConversation(Conversable player) {
this.player = player;

conversationFactory = new ConversationFactory(Carz.getInstance())
.withEscapeSequence("cancel")
.withTimeout(30)
.addConversationAbandonedListener(this)
.withFirstPrompt(getEntryPrompt());

player.sendRawMessage(ChatColor.GRAY + "Note: Enter 'cancel' to quit the conversation.");
}

public static void sendErrorMessage(ConversationContext context, String message) {
context.getForWhom().sendRawMessage(ChatColor.RED + message + ". Please try again...");
}

@Override
public void conversationAbandoned(ConversationAbandonedEvent event) {
if (!event.gracefulExit()) {
event.getContext().getForWhom().sendRawMessage(Carz.getPrefix() + "Conversation aborted...");
}
}

public void begin() {
Conversation convo = conversationFactory.buildConversation(player);
convo.begin();
}
}
Loading

0 comments on commit 1c1c78e

Please sign in to comment.