Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added ExtraValueCommand #424

Merged
merged 6 commits into from
Nov 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-7.2-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-7.3-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
package com.iridium.iridiumskyblock.commands;

import com.iridium.iridiumcore.utils.StringUtils;
import com.iridium.iridiumskyblock.IridiumSkyblock;
import com.iridium.iridiumskyblock.database.Island;
import com.iridium.iridiumskyblock.database.User;
import com.iridium.iridiumskyblock.utils.PlayerUtils;
import org.bukkit.Bukkit;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;

import java.time.Duration;
import java.util.*;

public class ExtraValueCommand extends Command {

/**
* The default constructor.
*/
public ExtraValueCommand() {
super(Collections.singletonList("extravalue"), "Set extra values of island", "%prefix% &7/is extravalue <add/remove/set> <player> <amount>", "iridiumskyblock.extravalue", false, Duration.ZERO);
}

/**
* Executes the command for the specified {@link CommandSender} with the provided arguments.
* Not called when the command execution was invalid (no permission, no player or command disabled).
* Gives extra values to specific player's island
*
* @param sender The CommandSender which executes this command
* @param args The arguments used with this command. They contain the sub-command
*/
@Override
public boolean execute(CommandSender sender, String[] args) {
if (args.length < 4) {
sender.sendMessage(StringUtils.color(syntax.replace("%prefix%", IridiumSkyblock.getInstance().getConfiguration().prefix)));
return false;
}

Player player = Bukkit.getPlayer(args[2]);
if (player != null) {
User user = IridiumSkyblock.getInstance().getUserManager().getUser(player);
Optional<Island> island = user.getIsland();
if (island.isPresent()) {
try {
double amount = Double.parseDouble(args[3]);
switch (args[1].toUpperCase(Locale.ENGLISH)) {
case "ADD":
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use SubCommands

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nevermind, thats dum

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was about to say the same... But I don't know, it would be a very small subcommand.

I can do a subcommand PR soon that adds subcommands for the other commands too.

island.get().setExtraValue(island.get().getExtraValue() + amount);
break;
case "REMOVE":
island.get().setExtraValue(island.get().getExtraValue() - amount);
break;
case "SET":
island.get().setExtraValue(amount);
break;
default:
sender.sendMessage(StringUtils.color(syntax.replace("%prefix%", IridiumSkyblock.getInstance().getConfiguration().prefix)));
return false;

}
sender.sendMessage(StringUtils.color(IridiumSkyblock.getInstance().getMessages().setExtraValue.replace("%player%", player.getName()).replace("%amount%", String.valueOf(island.get().getExtraValue())).replace("%prefix%", IridiumSkyblock.getInstance().getConfiguration().prefix)));
return true;
} catch (NumberFormatException e) {
sender.sendMessage(StringUtils.color(IridiumSkyblock.getInstance().getMessages().notANumber.replace("%prefix%", IridiumSkyblock.getInstance().getConfiguration().prefix)));
}
} else {
sender.sendMessage(StringUtils.color(IridiumSkyblock.getInstance().getMessages().userNoIsland.replace("%prefix%", IridiumSkyblock.getInstance().getConfiguration().prefix)));
}
} else {
sender.sendMessage(StringUtils.color(IridiumSkyblock.getInstance().getMessages().notAPlayer.replace("%prefix%", IridiumSkyblock.getInstance().getConfiguration().prefix)));
}
return false;
}

/**
* Handles tab-completion for this command.
*
* @param commandSender The CommandSender which tries to tab-complete
* @param command The command
* @param label The label of the command
* @param args The arguments already provided by the sender
* @return The list of tab completions for this command
*/
@Override
public List<String> onTabComplete(CommandSender commandSender, org.bukkit.command.Command command, String label, String[] args) {
if (args.length == 3) {
return PlayerUtils.getOnlinePlayerNames();
}

if (args.length == 2) {
return Arrays.asList("add", "remove", "set");
}

return Collections.emptyList();
}
dlsf marked this conversation as resolved.
Show resolved Hide resolved

}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public class Commands {
public DepositCommand depositCommand = new DepositCommand();
public EditWarpCommand editWarpCommand = new EditWarpCommand();
public ExpelCommand expelCommand = new ExpelCommand();
public ExtraValueCommand extraValueCommand = new ExtraValueCommand();
public FlyCommand flyCommand = new FlyCommand();
public HelpCommand helpCommand = new HelpCommand();
public HomeCommand homeCommand = new HomeCommand();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ public class Messages {
public String gaveBank = "%prefix% &7You gave %player% %amount% %item%.";
public String setBank = "%prefix% &7You set %player%'s %amount% to %item%.";
public String removedBank = "%prefix% &7You took %amount% %item%'s from %player%.";
public String setExtraValue = "%prefix% &7You have set %player%'s island extra value to %amount%.";
public String invalidMissionType = "%prefix% &7That is not a valid mission type.";
public String maxLevelReached = "%prefix% &7Maximum level reached.";
public String cannotAfford = "%prefix% &7You cannot afford this.";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ public final class Island {
@DatabaseField(columnName = "experience")
private int experience;

@DatabaseField(columnName = "extra_value")
private double extraValue;

@DatabaseField(columnName = "color", canBeNull = false)
private @NotNull Color color;

Expand Down Expand Up @@ -207,7 +210,7 @@ public LocalDateTime getCreateTime() {
*/
public double getValue() {
return valueCache.getCache(() -> {
double value = 0;
double value = extraValue;

List<IslandBlocks> islandBlocks = IridiumSkyblock.getInstance().getDatabaseManager().getIslandBlocksTableManager().getEntries(this);
List<IslandSpawners> islandSpawners = IridiumSkyblock.getInstance().getDatabaseManager().getIslandSpawnersTableManager().getEntries(this);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
@Getter
public class DatabaseManager {

private final int version = 2;
private final int version = 3;
private IslandTableManager islandTableManager;
private UserTableManager userTableManager;
private ForeignIslandTableManager<IslandBan, Integer> islandBanTableManager;
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/patch_1.sql
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ CREATE UNIQUE INDEX island_rewards_id_island_id_uindex ON island_rewards (id, is
CREATE UNIQUE INDEX island_spawners_id_spawner_type_island_id_uindex ON island_spawners (id, spawner_type, island_id);
CREATE UNIQUE INDEX island_trusted_id_user_island_id_uindex ON island_trusted (id, user, island_id);
CREATE UNIQUE INDEX island_upgrade_id_upgrade_island_id_uindex ON island_upgrade (id, upgrade, island_id);
CREATE UNIQUE INDEX island_warps_id_name_island_id_uindex ON island_warps (id, name, island_id);
CREATE UNIQUE INDEX island_warps_id_name_island_id_uindex ON island_warps (id, name, island_id);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Whats this aswell?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need more newlines

2 changes: 1 addition & 1 deletion src/main/resources/patch_2_mysql.sql
Original file line number Diff line number Diff line change
@@ -1 +1 @@
ALTER TABLE islands MODIFY name VARCHAR(512) NULL
ALTER TABLE islands MODIFY name VARCHAR(512) NULL
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Whats this?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did it, just added a newline because they are important

1 change: 1 addition & 0 deletions src/main/resources/patch_3.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE islands ADD COLUMN extra_value DOUBLE PRECISION;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldnt this be called _mysql? and where is the sqllite version?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It works for both MySQL and SQLite. The two different patches for MySQL and SQLite are optional

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SQLite doenst have the ALTER TABLE stuff though does it?
isnt that why we added the whole two different systems?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and why we had to do this mess for island names?

CREATE TEMPORARY TABLE islands_backup(id, name, home, visit, create_time, experience, color);
INSERT INTO islands_backup SELECT id, name, home, visit, create_time, experience, color FROM islands;
DROP TABLE islands;
CREATE TABLE islands(`id` INTEGER PRIMARY KEY AUTOINCREMENT , `name` VARCHAR , `home` VARCHAR , `visit` BOOLEAN , `create_time` BIGINT , `experience` INTEGER , `color` VARCHAR NOT NULL ,  UNIQUE (`name`));
INSERT INTO islands SELECT id, name, home, visit, create_time, experience, color FROM islands_backup;
DROP TABLE islands_backup;

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because creating tables works differently, I guess? The syntax checker said it's not possible

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Im confused? We are adding a new collum into the table, ALTER TABLE keyword doesnt work for sqllite, and I just tested it, this doesnt work. We need to do what we did for IslandNames

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SQLite also has certain ALTER TABLE statements: https://www.sqlite.org/lang_altertable.html