Skip to content

Commit 1c1c78e

Browse files
committed
Refactors Cars to inherit CarDetails, which are setup in the config.yml
Car Types are unique names which have an associated CarDetails (standard, racecar, etc.) Conversation to allow for creation of CarType Fixes and update dependencies
1 parent 3ce45a6 commit 1c1c78e

15 files changed

+387
-147
lines changed

pom.xml

+4-4
Original file line numberDiff line numberDiff line change
@@ -53,14 +53,14 @@
5353
<dependency>
5454
<groupId>org.spigotmc</groupId>
5555
<artifactId>spigot-api</artifactId>
56-
<version>1.14.2-R0.1-SNAPSHOT</version>
56+
<version>1.15.2-R0.1-SNAPSHOT</version>
5757
<scope>provided</scope>
5858
</dependency>
5959
<!--Bukkit API-->
6060
<dependency>
6161
<groupId>org.bukkit</groupId>
6262
<artifactId>bukkit</artifactId>
63-
<version>1.14.2-R0.1-SNAPSHOT</version>
63+
<version>1.15.2-R0.1-SNAPSHOT</version>
6464
<scope>provided</scope>
6565
</dependency>
6666
<!--Vault API-->
@@ -81,7 +81,7 @@
8181
<dependency>
8282
<groupId>org.bstats</groupId>
8383
<artifactId>bstats-bukkit</artifactId>
84-
<version>1.4</version>
84+
<version>1.7</version>
8585
<scope>compile</scope>
8686
</dependency>
8787
<!-- Updater -->
@@ -121,7 +121,7 @@
121121
<plugin>
122122
<groupId>org.apache.maven.plugins</groupId>
123123
<artifactId>maven-assembly-plugin</artifactId>
124-
<version>3.1.1</version>
124+
<version>3.2.0</version>
125125
<configuration>
126126
<descriptorRefs>
127127
<descriptorRef>jar-with-dependencies</descriptorRef>

src/main/java/io/github/a5h73y/Carz.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public void onEnable() {
4545
setupPlugins();
4646

4747
getLogger().info("Enabled Carz v" + getDescription().getVersion());
48-
new Metrics(this);
48+
new Metrics(this, 42269);
4949
updatePlugin();
5050
}
5151

src/main/java/io/github/a5h73y/commands/CarzConsoleCommands.java

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package io.github.a5h73y.commands;
22

33
import io.github.a5h73y.Carz;
4+
import io.github.a5h73y.conversation.CreateCarType;
45
import io.github.a5h73y.enums.Commands;
56
import io.github.a5h73y.other.Utils;
67
import io.github.a5h73y.utility.TranslationUtils;
@@ -9,6 +10,7 @@
910
import org.bukkit.command.Command;
1011
import org.bukkit.command.CommandExecutor;
1112
import org.bukkit.command.CommandSender;
13+
import org.bukkit.command.ConsoleCommandSender;
1214
import org.bukkit.entity.Player;
1315

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

2527
@Override
2628
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
27-
if (sender instanceof Player) {
29+
if (sender instanceof Player || !(sender instanceof ConsoleCommandSender)) {
2830
sender.sendMessage(Carz.getPrefix() + "Use /carz instead.");
2931
return false;
3032
}
@@ -72,6 +74,10 @@ public boolean onCommand(CommandSender sender, Command command, String label, St
7274
TranslationUtils.sendTranslation("Carz.ConfigReloaded", sender);
7375
break;
7476

77+
case "createtype":
78+
new CreateCarType((ConsoleCommandSender) sender).begin();
79+
break;
80+
7581
case "cmds":
7682
sender.sendMessage("/carzc spawn (player)");
7783
sender.sendMessage("/carzc addCB");

src/main/java/io/github/a5h73y/configuration/Settings.java

+10-4
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ private void setupConfig() {
7474
carz.getConfig().addDefault("Other.DestroyInLiquid", true);
7575
carz.getConfig().addDefault("Other.UsePermissions", true);
7676
carz.getConfig().addDefault("Other.UseEffects", true);
77+
carz.getConfig().addDefault("Other.UseAutoTabCompletion", true);
7778
carz.getConfig().addDefault("Other.UpdateCheck", true);
7879

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

86+
carz.getConfig().addDefault("CarTypes.Default.StartMaxSpeed", 1.0);
87+
carz.getConfig().addDefault("CarTypes.Default.Acceleration", 1.0);
88+
carz.getConfig().addDefault("CarTypes.Default.FuelUsage", 1.0);
89+
carz.getConfig().addDefault("CarTypes.Default.FillMaterial", "AIR");
90+
8591
carz.getConfig().options().copyDefaults(true);
8692
carz.saveConfig();
8793
}
@@ -179,19 +185,19 @@ public boolean isFuelScaleCost() {
179185
return carz.getConfig().getBoolean("Fuel.ScaleCost");
180186
}
181187

182-
public Double getStartSpeed() {
188+
public double getStartSpeed() {
183189
return carz.getConfig().getDouble("Speed.Start");
184190
}
185191

186-
public Double getUpgradeSpeed() {
192+
public double getUpgradeIncrement() {
187193
return carz.getConfig().getDouble("Speed.Upgrade.Increment");
188194
}
189195

190-
public Double getUpgradeMaxSpeed() {
196+
public double getUpgradeMaxSpeed() {
191197
return carz.getConfig().getDouble("Speed.Upgrade.Max");
192198
}
193199

194-
public Double getClimbBlockStrength() {
200+
public double getClimbBlockStrength() {
195201
return carz.getConfig().getDouble("ClimbBlocks.Strength");
196202
}
197203

src/main/java/io/github/a5h73y/controllers/CarController.java

+34-11
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@
22

33
import java.util.HashMap;
44
import java.util.Map;
5+
import java.util.Set;
56

67
import io.github.a5h73y.Carz;
78
import io.github.a5h73y.enums.Permissions;
89
import io.github.a5h73y.model.Car;
9-
import io.github.a5h73y.model.StandardCar;
10+
import io.github.a5h73y.model.CarDetails;
1011
import io.github.a5h73y.other.Utils;
1112
import io.github.a5h73y.utility.EffectUtils;
1213
import io.github.a5h73y.utility.PermissionUtils;
@@ -21,6 +22,8 @@
2122
*/
2223
public class CarController {
2324

25+
public static final String DEFAULT_CAR = "Default";
26+
2427
private final Carz carz;
2528

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

35+
private final Map<String, CarDetails> carTypes = new HashMap<>();
36+
3237
public CarController(Carz carz) {
3338
this.carz = carz;
39+
populateCarTypes();
40+
}
41+
42+
private void populateCarTypes() {
43+
Set<String> allCarTypes = carz.getConfig().getConfigurationSection("CarTypes").getKeys(false);
44+
45+
for (String carType : allCarTypes) {
46+
double startSpeed = carz.getConfig().getDouble("CarTypes." + carType + ".StartMaxSpeed");
47+
double maxSpeed = carz.getConfig().getDouble("CarTypes." + carType + ".MaxUpgradeSpeed");
48+
double acceleration = carz.getConfig().getDouble("CarTypes." + carType + ".Acceleration");
49+
double fuelUsage = carz.getConfig().getDouble("CarTypes." + carType + ".FuelUsage");
50+
String fillMaterial = carz.getConfig().getString("CarTypes." + carType + ".FillMaterial");
51+
carTypes.put(carType, new CarDetails(startSpeed, maxSpeed, acceleration, fuelUsage, fillMaterial));
52+
}
3453
}
3554

3655
/**
@@ -40,7 +59,7 @@ public CarController(Carz carz) {
4059
* @param owner is the player the registered owner?
4160
*/
4261
public void startDriving(String playerName, Integer carId, boolean owner) {
43-
Car car = getOrCreateCar(carId, new StandardCar(carId));
62+
Car car = getOrCreateCar(carId, DEFAULT_CAR);
4463
if (owner) {
4564
car.setOwner(playerName);
4665
}
@@ -58,8 +77,8 @@ public void startDriving(String playerName, Integer carId) {
5877
* @param carType
5978
* @return matching or new Car
6079
*/
61-
public Car getOrCreateCar(Integer entityId, Car carType) {
62-
return entityIdToCar.computeIfAbsent(entityId, k -> carType);
80+
public Car getOrCreateCar(Integer entityId, String carType) {
81+
return entityIdToCar.computeIfAbsent(entityId, k -> new Car(entityId, carType));
6382
}
6483

6584
/**
@@ -68,7 +87,7 @@ public Car getOrCreateCar(Integer entityId, Car carType) {
6887
* @param entityId
6988
*/
7089
public Car getOrCreateCar(Integer entityId) {
71-
return getOrCreateCar(entityId, new StandardCar(entityId));
90+
return getOrCreateCar(entityId, DEFAULT_CAR);
7291
}
7392

7493
/**
@@ -168,7 +187,7 @@ public void upgradeCarSpeed(Player player) {
168187
upgradeCarSpeed(car);
169188
EffectUtils.playEffect(player, Effect.ZOMBIE_CHEW_WOODEN_DOOR);
170189
player.sendMessage(TranslationUtils.getTranslation("Car.UpgradeSpeed")
171-
.replace("%SPEED%", car.getMaxSpeed().toString()));
190+
.replace("%SPEED%", String.valueOf(car.getMaxSpeed())));
172191

173192
EffectUtils.createUpgradeEffect((Vehicle) player.getVehicle());
174193
}
@@ -180,14 +199,18 @@ public void upgradeCarSpeed(Player player) {
180199
* @param car
181200
*/
182201
private void upgradeCarSpeed(Car car) {
183-
Double currentSpeed = car.getMaxSpeed();
184-
Double upgradeBy = carz.getSettings().getUpgradeSpeed();
185-
Double maxSpeed = carz.getSettings().getUpgradeMaxSpeed();
202+
double currentMax = car.getCarDetails().getStartMaxSpeed();
203+
double maxSpeed = car.getCarDetails().getMaxUpgradeSpeed();
204+
double upgradeBy = carz.getSettings().getUpgradeIncrement();
186205

187-
if ((currentSpeed + upgradeBy) > maxSpeed) {//&& !event.getPlayer().hasPermission("Carz.Admin"))
206+
if ((currentMax + upgradeBy) > maxSpeed) {//&& !event.getPlayer().hasPermission("Carz.Admin"))
188207
return;
189208
}
190209

191-
car.setMaxSpeed(currentSpeed + upgradeBy);
210+
car.setMaxSpeed(currentMax + upgradeBy);
211+
}
212+
213+
public Map<String, CarDetails> getCarTypes() {
214+
return carTypes;
192215
}
193216
}

src/main/java/io/github/a5h73y/controllers/FuelController.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public void displayFuelLevel(Player player) {
6060
}
6161

6262
public void refuel(Car car) {
63-
car.setFuel(MAX_FUEL);
63+
car.setCurrentFuel(MAX_FUEL);
6464
}
6565

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

8181
sb.append(ChatColor.RED);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package io.github.a5h73y.conversation;
2+
3+
import io.github.a5h73y.Carz;
4+
import org.bukkit.ChatColor;
5+
import org.bukkit.conversations.Conversable;
6+
import org.bukkit.conversations.Conversation;
7+
import org.bukkit.conversations.ConversationAbandonedEvent;
8+
import org.bukkit.conversations.ConversationAbandonedListener;
9+
import org.bukkit.conversations.ConversationContext;
10+
import org.bukkit.conversations.ConversationFactory;
11+
import org.bukkit.conversations.Prompt;
12+
13+
public abstract class CarzConversation implements ConversationAbandonedListener {
14+
15+
private ConversationFactory conversationFactory;
16+
private Conversable player;
17+
18+
public abstract Prompt getEntryPrompt();
19+
20+
public CarzConversation(Conversable player) {
21+
this.player = player;
22+
23+
conversationFactory = new ConversationFactory(Carz.getInstance())
24+
.withEscapeSequence("cancel")
25+
.withTimeout(30)
26+
.addConversationAbandonedListener(this)
27+
.withFirstPrompt(getEntryPrompt());
28+
29+
player.sendRawMessage(ChatColor.GRAY + "Note: Enter 'cancel' to quit the conversation.");
30+
}
31+
32+
public static void sendErrorMessage(ConversationContext context, String message) {
33+
context.getForWhom().sendRawMessage(ChatColor.RED + message + ". Please try again...");
34+
}
35+
36+
@Override
37+
public void conversationAbandoned(ConversationAbandonedEvent event) {
38+
if (!event.gracefulExit()) {
39+
event.getContext().getForWhom().sendRawMessage(Carz.getPrefix() + "Conversation aborted...");
40+
}
41+
}
42+
43+
public void begin() {
44+
Conversation convo = conversationFactory.buildConversation(player);
45+
convo.begin();
46+
}
47+
}

0 commit comments

Comments
 (0)