Skip to content

Commit

Permalink
add setaddmount and setprice commands
Browse files Browse the repository at this point in the history
  • Loading branch information
Brokkonaut committed Aug 28, 2021
1 parent 1a57c66 commit eff32a1
Show file tree
Hide file tree
Showing 6 changed files with 166 additions and 2 deletions.
4 changes: 4 additions & 0 deletions src/main/java/com/Acrobot/ChestShop/ChestShop.java
Expand Up @@ -19,6 +19,8 @@
import com.Acrobot.Breeze.Configuration.Configuration;
import com.Acrobot.ChestShop.Commands.Give;
import com.Acrobot.ChestShop.Commands.ItemInfo;
import com.Acrobot.ChestShop.Commands.SetAmount;
import com.Acrobot.ChestShop.Commands.SetPrice;
import com.Acrobot.ChestShop.Commands.Toggle;
import com.Acrobot.ChestShop.Commands.Version;
import com.Acrobot.ChestShop.Configuration.Messages;
Expand Down Expand Up @@ -136,6 +138,8 @@ public void onEnable() {
getCommand("csVersion").setExecutor(new Version());
getCommand("csGive").setExecutor(new Give());
getCommand("cstoggle").setExecutor(new Toggle());
getCommand("csSetPrice").setExecutor(new SetPrice());
getCommand("csSetAmount").setExecutor(new SetAmount());
}

private void handleMigrations() {
Expand Down
70 changes: 70 additions & 0 deletions src/main/java/com/Acrobot/ChestShop/Commands/SetAmount.java
@@ -0,0 +1,70 @@
package com.Acrobot.ChestShop.Commands;

import com.Acrobot.ChestShop.Configuration.Messages;
import com.Acrobot.ChestShop.Signs.ChestShopSign;
import static com.Acrobot.ChestShop.Permission.ADMIN;

import com.Acrobot.ChestShop.ChestShop;
import com.Acrobot.ChestShop.Permission;
import org.bukkit.block.Block;
import org.bukkit.block.Sign;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.event.block.SignChangeEvent;
import org.bukkit.util.RayTraceResult;

public class SetAmount implements CommandExecutor {
@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
if (!(sender instanceof Player)) {
return false;
}

if (!Permission.has(sender, Permission.SET_AMOUNT_COMMAND)) {
sender.sendMessage(Messages.ACCESS_DENIED);
return true;
}

Player player = (Player) sender;
RayTraceResult result = player.rayTraceBlocks(8);
Block signBlock = null;
if (result != null) {
signBlock = result.getHitBlock();
}
if (signBlock == null || !ChestShopSign.isValid(signBlock)) {
sender.sendMessage(Messages.MUST_LOOK_AT_SHOP_SIGN);
return true;
}

Sign sign = (Sign) signBlock.getState();
if (!ChestShopSign.canAccess(player, sign) && !Permission.has(player, ADMIN)) {
sender.sendMessage(Messages.ACCESS_DENIED);
return true;
}

String newAmountLine = String.join(" ", args);
String[] line = sign.getLines();
line[1] = newAmountLine;
if (!ChestShopSign.isValidPreparedSign(line)) {
sender.sendMessage(Messages.INVALID_AMOUNT_LINE);
return true;
}

SignChangeEvent event = new SignChangeEvent(signBlock, player, line);
ChestShop.getPlugin().getServer().getPluginManager().callEvent(event);
if (event.isCancelled()) {
sender.sendMessage(Messages.SHOP_UPDATE_FAILED);
return true;
}

line = event.getLines();

for (int i = 0; i < line.length; i++) {
sign.setLine(i, line[i]);
}
sign.update();
return true;
}
}
70 changes: 70 additions & 0 deletions src/main/java/com/Acrobot/ChestShop/Commands/SetPrice.java
@@ -0,0 +1,70 @@
package com.Acrobot.ChestShop.Commands;

import com.Acrobot.ChestShop.Configuration.Messages;
import com.Acrobot.ChestShop.Signs.ChestShopSign;
import static com.Acrobot.ChestShop.Permission.ADMIN;

import com.Acrobot.ChestShop.ChestShop;
import com.Acrobot.ChestShop.Permission;
import org.bukkit.block.Block;
import org.bukkit.block.Sign;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.event.block.SignChangeEvent;
import org.bukkit.util.RayTraceResult;

public class SetPrice implements CommandExecutor {
@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
if (!(sender instanceof Player)) {
return false;
}

if (!Permission.has(sender, Permission.SET_PRICE_COMMAND)) {
sender.sendMessage(Messages.ACCESS_DENIED);
return true;
}

Player player = (Player) sender;
RayTraceResult result = player.rayTraceBlocks(8);
Block signBlock = null;
if (result != null) {
signBlock = result.getHitBlock();
}
if (signBlock == null || !ChestShopSign.isValid(signBlock)) {
sender.sendMessage(Messages.MUST_LOOK_AT_SHOP_SIGN);
return true;
}

Sign sign = (Sign) signBlock.getState();
if (!ChestShopSign.canAccess(player, sign) && !Permission.has(player, ADMIN)) {
sender.sendMessage(Messages.ACCESS_DENIED);
return true;
}

String newPriceLine = String.join(" ", args);
String[] line = sign.getLines();
line[2] = newPriceLine;
if (!ChestShopSign.isValidPreparedSign(line)) {
sender.sendMessage(Messages.INVALID_PRICE_LINE);
return true;
}

SignChangeEvent event = new SignChangeEvent(signBlock, player, line);
ChestShop.getPlugin().getServer().getPluginManager().callEvent(event);
if (event.isCancelled()) {
sender.sendMessage(Messages.SHOP_UPDATE_FAILED);
return true;
}

line = event.getLines();

for (int i = 0; i < line.length; i++) {
sign.setLine(i, line[i]);
}
sign.update();
return true;
}
}
Expand Up @@ -78,6 +78,12 @@ public class Messages {
public static String TOGGLE_MESSAGES_OFF = "You will no longer receive messages from your shop(s).";
public static String TOGGLE_MESSAGES_ON = "You will now receive messages from your shop(s).";

@PrecededBySpace
public static String MUST_LOOK_AT_SHOP_SIGN = "You must look at a shop sign when using this command.";
public static String INVALID_PRICE_LINE = "You have specified an invalid price!";
public static String INVALID_AMOUNT_LINE = "You have specified an invalid amount!";
public static String SHOP_UPDATE_FAILED = "This shop cannot be edited.";

public static String prefix(String message) {
return PREFIX + message;
}
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/com/Acrobot/ChestShop/Permission.java
Expand Up @@ -19,7 +19,9 @@ public enum Permission {

NOFEE("ChestShop.nofee"), DISCOUNT("ChestShop.discount."),

NOTIFY_TOGGLE("ChestShop.toggle");
NOTIFY_TOGGLE("ChestShop.toggle"), SET_PRICE_COMMAND("ChestShop.setpricecommand"),

SET_AMOUNT_COMMAND("ChestShop.setamountcommand");

private final String permission;

Expand Down
14 changes: 13 additions & 1 deletion src/main/resources/plugin.yml
Expand Up @@ -38,7 +38,13 @@ commands:
cstoggle:
description: Toggle messages to the owner of a shop
usage: /<command>

csSetPrice:
description: Edit the price of a shop
usage: /<command>
csSetAmount:
description: Edit the amount of a shop
usage: /<command>

permissions:
ChestShop.*:
description: Gives access to all ChestShop permissions
Expand All @@ -52,6 +58,8 @@ permissions:
ChestShop.shop.create: true
ChestShop.shop.buy: true
ChestShop.shop.sell: true
ChestShop.setpricecommand: true
ChestShop.setamountcommand: true
default: true
ChestShop.shop.create:
description: Allows the user to create a shop that sells and buys any item
Expand All @@ -62,6 +70,10 @@ permissions:
description: Allows the user to create a shop that sells any item
ChestShop.shop.create.sell:
description: Allows the user to create a shop that buy any item
ChestShop.setpricecommand:
description: Allows the user modify the price of existing own shops
ChestShop.setamountcommand:
description: Allows the user modify the amount of existing own shops
ChestShop.shop.create.(itemType):
description: Allows user to create a shop that sells item with itemType like in the permission node (replace (itemType) with Material item name)
ChestShop.shop.buy.(itemType):
Expand Down

0 comments on commit eff32a1

Please sign in to comment.