Skip to content
Open
3 changes: 3 additions & 0 deletions docs/permissions.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ The following permissions can be used to restrict functionality within the plugi
* **coreprotect.consumer** *(default: op)*
Allows access to the CoreProtect consumer command.
 
* **coreprotect.give** *(default: op)*
Allows access to the CoreProtect give command.
 
* **coreprotect.networking** *(default: op)*
Allows access to the CoreProtect networking API.

Expand Down
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
<modelVersion>4.0.0</modelVersion>
<groupId>net.coreprotect</groupId>
<artifactId>CoreProtect</artifactId>
<version>23.0</version>
<version>23.1</version>
<properties>
<project.branch></project.branch>
<project.branch>development</project.branch>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<skipTests>true</skipTests>
<maven.compiler.source>11</maven.compiler.source>
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/net/coreprotect/command/CommandHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@ else if (user.hasPermission("coreprotect.consumer") && corecommand.equals("consu
else if (user.hasPermission("coreprotect.networking") && corecommand.equals("network-debug")) {
permission = true;
}
else if (user.hasPermission("coreprotect.give") && corecommand.equals("give")) {
permission = true;
}
}

if (corecommand.equals("rollback") || corecommand.equals("restore") || corecommand.equals("rb") || corecommand.equals("rs") || corecommand.equals("ro") || corecommand.equals("re")) {
Expand Down Expand Up @@ -119,6 +122,9 @@ else if (corecommand.equals("consumer")) {
else if (corecommand.equals("network-debug")) {
NetworkDebugCommand.runCommand(user, permission, argumentArray);
}
else if (corecommand.equals("give")) {
GiveCommand.runCommand(user, command, permission, argumentArray);
}
else if (corecommand.equals("migrate-db")) {
if (!VersionUtils.validDonationKey()) {
Chat.sendMessage(user, Color.DARK_AQUA + "CoreProtect " + Color.WHITE + "- " + Phrase.build(Phrase.DONATION_KEY_REQUIRED));
Expand Down
11 changes: 4 additions & 7 deletions src/main/java/net/coreprotect/command/CommandParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,11 @@
import java.util.Map;
import java.util.Set;

import net.coreprotect.command.parser.*;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.command.CommandSender;

import net.coreprotect.command.parser.ActionParser;
import net.coreprotect.command.parser.LocationParser;
import net.coreprotect.command.parser.MaterialParser;
import net.coreprotect.command.parser.TimeParser;
import net.coreprotect.command.parser.UserParser;
import net.coreprotect.command.parser.WorldParser;

/**
* Main parser class for CoreProtect commands.
* Delegates to specialized parser classes for specific functionality.
Expand Down Expand Up @@ -326,4 +320,7 @@ private static String timeString(BigDecimal input) {
return input.stripTrailingZeros().toPlainString();
}

protected static Integer parseGivableItemId(String[] inputArguments) {
return GivableItemIdParser.parseGivableItemId(inputArguments);
}
}
40 changes: 40 additions & 0 deletions src/main/java/net/coreprotect/command/GiveCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package net.coreprotect.command;

import net.coreprotect.language.Phrase;
import net.coreprotect.utility.Chat;
import net.coreprotect.utility.ChatMessage;
import net.coreprotect.utility.Color;
import net.coreprotect.utility.ItemUtils;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;

public class GiveCommand {
public static void runCommand(CommandSender sender, Command command, boolean permission, String[] args) {
if (!permission) {
Chat.sendMessage(sender, new ChatMessage(Phrase.build(Phrase.NO_PERMISSION)).build());
return;
}

Integer itemId = CommandParser.parseGivableItemId(args);
if (itemId == null) {
Chat.sendMessage(sender, new ChatMessage(Phrase.build(Phrase.MISSING_PARAMETERS, Color.WHITE, "/" + command.getName() + " give <itemId>")).build());
return;
}

ItemStack item = ItemUtils.getGivableItem(itemId);
if (item == null) {
Chat.sendMessage(sender, new ChatMessage(Phrase.build(Phrase.INVALID_ITEM_ID)).build());
return;
}

if (!(sender instanceof Player)) {
Chat.sendMessage(sender, new ChatMessage(Phrase.build(Phrase.ACTION_NOT_SUPPORTED)).build());
return;
}

Player player = (Player) sender;
player.getInventory().addItem(item);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -89,382 +89,384 @@
this.count = count;
}

@Override
public void run() {
try (Connection connection = Database.getConnection(true)) {
ConfigHandler.lookupThrottle.put(player.getName(), new Object[] { true, System.currentTimeMillis() });

List<String> uuidList = new ArrayList<>();
Location finalLocation = location;
boolean exists = false;
String bc = x + "." + y + "." + z + "." + worldId + "." + timeStart + "." + timeEnd + "." + noisy + "." + excluded + "." + restricted + "." + argWorldId + "." + displayResults;
ConfigHandler.lookupCommand.put(player.getName(), bc);
ConfigHandler.lookupPage.put(player.getName(), page);
ConfigHandler.lookupTime.put(player.getName(), rtime);
ConfigHandler.lookupType.put(player.getName(), 5);
ConfigHandler.lookupElist.put(player.getName(), excludedBlocks);
ConfigHandler.lookupEUserlist.put(player.getName(), excludedUsers);
ConfigHandler.lookupBlist.put(player.getName(), blockList);
ConfigHandler.lookupUlist.put(player.getName(), rollbackUsers);
ConfigHandler.lookupAlist.put(player.getName(), actions);
ConfigHandler.lookupRadius.put(player.getName(), radius);

if (connection != null) {
Statement statement = connection.createStatement();
String baduser = "";
for (String check : rollbackUsers) {
if ((!check.equals("#global") && !check.equals("#container")) || actions.contains(9)) {
exists = PlayerLookup.playerExists(connection, check);
if (!exists) {
baduser = check;
break;
}
else if (actions.contains(9)) {
if (ConfigHandler.uuidCache.get(check.toLowerCase(Locale.ROOT)) != null) {
String uuid = ConfigHandler.uuidCache.get(check.toLowerCase(Locale.ROOT));
uuidList.add(uuid);
}
}
}
else {
exists = true;
}
}
if (exists) {
for (String check : excludedUsers) {
if (!check.equals("#global") && !check.equals("#hopper")) {
exists = PlayerLookup.playerExists(connection, check);
if (!exists) {
baduser = check;
break;
}
}
else if (check.equals("#global")) {
baduser = "#global";
exists = false;
}
}
}

if (exists) {
List<String> userList = new ArrayList<>();
if (!actions.contains(9)) {
userList = rollbackUsers;
}

int unixtimestamp = (int) (System.currentTimeMillis() / 1000L);
boolean restrict_world = false;
if (radius != null) {
restrict_world = true;
}
if (finalLocation == null) {
restrict_world = false;
}
if (argWorldId > 0) {
restrict_world = true;
finalLocation = new Location(Bukkit.getServer().getWorld(WorldUtils.getWorldName(argWorldId)), x, y, z);
}
else if (finalLocation != null) {
finalLocation = new Location(Bukkit.getServer().getWorld(WorldUtils.getWorldName(worldId)), x, y, z);
}

Long[] rowData = new Long[] { 0L, 0L, 0L, 0L };
long rowMax = (long) page * displayResults;
long pageStart = rowMax - displayResults;
long rows = 0L;
boolean checkRows = true;

if (typeLookup == 5 && page > 1) {
rowData = ConfigHandler.lookupRows.get(player.getName());
rows = rowData[3];

if (pageStart < rows) {
checkRows = false;
}
}

if (checkRows) {
rows = Lookup.countLookupRows(statement, player, uuidList, userList, blockList, excludedBlocks, excludedUsers, actions, finalLocation, radius, rowData, timeStart, timeEnd, restrict_world, true);
rowData[3] = rows;
ConfigHandler.lookupRows.put(player.getName(), rowData);
}
if (count) {
String row_format = NumberFormat.getInstance().format(rows);
Chat.sendMessage(player, Color.DARK_AQUA + "CoreProtect " + Color.WHITE + "- " + Phrase.build(Phrase.LOOKUP_ROWS_FOUND, row_format, (rows == 1 ? Selector.FIRST : Selector.SECOND)));
}
else if (pageStart < rows) {
List<String[]> lookupList = Lookup.performPartialLookup(statement, player, uuidList, userList, blockList, excludedBlocks, excludedUsers, actions, finalLocation, radius, rowData, timeStart, timeEnd, (int) pageStart, displayResults, restrict_world, true);

Chat.sendMessage(player, Color.WHITE + "----- " + Color.DARK_AQUA + Phrase.build(Phrase.LOOKUP_HEADER, "CoreProtect" + Color.WHITE + " | " + Color.DARK_AQUA) + Color.WHITE + " -----");
if (actions.contains(6) || actions.contains(7)) { // Chat/command
for (String[] data : lookupList) {
String time = data[0];
String dplayer = data[1];
String message = data[2];
String timeago = ChatUtils.getTimeSince(Integer.parseInt(time), unixtimestamp, true);
Chat.sendComponent(player, timeago + " " + Color.WHITE + "- " + Color.DARK_AQUA + dplayer + ": " + Color.WHITE, message);
if (PluginChannelHandshakeListener.getInstance().isPluginChannelPlayer(player)) {
int wid = Integer.parseInt(data[3]);
int dataX = Integer.parseInt(data[4]);
int dataY = Integer.parseInt(data[5]);
int dataZ = Integer.parseInt(data[6]);
PluginChannelListener.getInstance().sendMessageData(player, Integer.parseInt(time), dplayer, message, false, dataX, dataY, dataZ, wid);
}
}
}
else if (actions.contains(8)) { // login/logouts
for (String[] data : lookupList) {
String time = data[0];
String dplayer = data[1];
int wid = Integer.parseInt(data[2]);
int dataX = Integer.parseInt(data[3]);
int dataY = Integer.parseInt(data[4]);
int dataZ = Integer.parseInt(data[5]);
int action = Integer.parseInt(data[6]);
String timeago = ChatUtils.getTimeSince(Integer.parseInt(time), unixtimestamp, true);
int timeLength = 50 + (ChatUtils.getTimeSince(Integer.parseInt(time), unixtimestamp, false).replaceAll("[^0-9]", "").length() * 6);
String leftPadding = Color.BOLD + Strings.padStart("", 10, ' ');
if (timeLength % 4 == 0) {
leftPadding = Strings.padStart("", timeLength / 4, ' ');
}
else {
leftPadding = leftPadding + Color.WHITE + Strings.padStart("", (timeLength - 50) / 4, ' ');
}

String tag = (action != 0 ? Color.GREEN + "+" : Color.RED + "-");
Chat.sendComponent(player, timeago + " " + tag + " " + Color.DARK_AQUA + Phrase.build(Phrase.LOOKUP_LOGIN, Color.DARK_AQUA + dplayer + Color.WHITE, (action != 0 ? Selector.FIRST : Selector.SECOND)));
Chat.sendComponent(player, Color.WHITE + leftPadding + Color.GREY + "^ " + ChatUtils.getCoordinates(command.getName(), wid, dataX, dataY, dataZ, true, true) + "");
PluginChannelListener.getInstance().sendInfoData(player, Integer.parseInt(time), Phrase.LOOKUP_LOGIN, (action != 0 ? Selector.FIRST : Selector.SECOND), dplayer, -1, dataX, dataY, dataZ, wid);
}
}
else if (actions.contains(9)) { // username-changes
for (String[] data : lookupList) {
String time = data[0];
String user = ConfigHandler.uuidCacheReversed.get(data[1]);
String username = data[2];
String timeago = ChatUtils.getTimeSince(Integer.parseInt(time), unixtimestamp, true);
Chat.sendComponent(player, timeago + " " + Color.WHITE + "- " + Phrase.build(Phrase.LOOKUP_USERNAME, Color.DARK_AQUA + user + Color.WHITE, Color.DARK_AQUA + username + Color.WHITE));
PluginChannelListener.getInstance().sendUsernameData(player, Integer.parseInt(time), user, username);
}
}
else if (actions.contains(10)) { // sign messages
for (String[] data : lookupList) {
String time = data[0];
String dplayer = data[1];
int wid = Integer.parseInt(data[2]);
int dataX = Integer.parseInt(data[3]);
int dataY = Integer.parseInt(data[4]);
int dataZ = Integer.parseInt(data[5]);
String message = data[6];
String timeago = ChatUtils.getTimeSince(Integer.parseInt(time), unixtimestamp, true);
int timeLength = 50 + (ChatUtils.getTimeSince(Integer.parseInt(time), unixtimestamp, false).replaceAll("[^0-9]", "").length() * 6);
String leftPadding = Color.BOLD + Strings.padStart("", 10, ' ');
if (timeLength % 4 == 0) {
leftPadding = Strings.padStart("", timeLength / 4, ' ');
}
else {
leftPadding = leftPadding + Color.WHITE + Strings.padStart("", (timeLength - 50) / 4, ' ');
}

Chat.sendComponent(player, timeago + " " + Color.WHITE + "- " + Color.DARK_AQUA + dplayer + ": " + Color.WHITE, message);
Chat.sendComponent(player, Color.WHITE + leftPadding + Color.GREY + "^ " + ChatUtils.getCoordinates(command.getName(), wid, dataX, dataY, dataZ, true, true) + "");
PluginChannelListener.getInstance().sendMessageData(player, Integer.parseInt(time), dplayer, message, true, dataX, dataY, dataZ, wid);
}
}
else if (actions.contains(4) && actions.contains(11)) { // inventory transactions
for (String[] data : lookupList) {
String time = data[0];
String dplayer = data[1];
int dtype = Integer.parseInt(data[5]);
int ddata = Integer.parseInt(data[6]);
int daction = Integer.parseInt(data[7]);
int amount = Integer.parseInt(data[10]);
int wid = Integer.parseInt(data[9]);
int dataX = Integer.parseInt(data[2]);
int dataY = Integer.parseInt(data[3]);
int dataZ = Integer.parseInt(data[4]);
String rbd = ((Integer.parseInt(data[8]) == 2 || Integer.parseInt(data[8]) == 3) ? Color.STRIKETHROUGH : "");
String timeago = ChatUtils.getTimeSince(Integer.parseInt(time), unixtimestamp, true);
Material blockType = ItemUtils.itemFilter(MaterialUtils.getType(dtype), (Integer.parseInt(data[13]) == 0));
String dname = StringUtils.nameFilter(blockType.name().toLowerCase(Locale.ROOT), ddata);
byte[] metadata = data[11] == null ? null : data[11].getBytes(StandardCharsets.ISO_8859_1);
String tooltip = ItemUtils.getEnchantments(metadata, dtype, amount);
Integer itemId = ItemUtils.makeGivableItem(ItemUtils.getItemStack(metadata, dtype, amount));

String selector = Selector.FIRST;
String tag = Color.WHITE + "-";
if (daction == 2 || daction == 3) { // LOOKUP_ITEM
selector = (daction != 2 ? Selector.FIRST : Selector.SECOND);
tag = (daction != 2 ? Color.GREEN + "+" : Color.RED + "-");
}
else if (daction == 4 || daction == 5) { // LOOKUP_STORAGE
selector = (daction == 4 ? Selector.FIRST : Selector.SECOND);
tag = (daction == 4 ? Color.GREEN + "+" : Color.RED + "-");
}
else if (daction == 6 || daction == 7) { // LOOKUP_PROJECTILE
selector = Selector.SECOND;
tag = Color.RED + "-";
}
else if (daction == ItemLogger.ITEM_BREAK || daction == ItemLogger.ITEM_DESTROY || daction == ItemLogger.ITEM_CREATE) {
selector = (daction == ItemLogger.ITEM_CREATE ? Selector.FIRST : Selector.SECOND);
tag = (daction == ItemLogger.ITEM_CREATE ? Color.GREEN + "+" : Color.RED + "-");
}
else if (daction == ItemLogger.ITEM_SELL || daction == ItemLogger.ITEM_BUY) { // LOOKUP_TRADE
selector = (daction == ItemLogger.ITEM_BUY ? Selector.FIRST : Selector.SECOND);
tag = (daction == ItemLogger.ITEM_BUY ? Color.GREEN + "+" : Color.RED + "-");
}
else { // LOOKUP_CONTAINER
selector = (daction == 0 ? Selector.FIRST : Selector.SECOND);
tag = (daction == 0 ? Color.GREEN + "+" : Color.RED + "-");
}

Chat.sendComponent(player, timeago + " " + tag + " " + Phrase.build(Phrase.LOOKUP_CONTAINER, Color.DARK_AQUA + rbd + dplayer + Color.WHITE + rbd, "x" + amount, ChatUtils.createTooltip(Color.DARK_AQUA + rbd + dname, tooltip) + Color.WHITE, selector));
Chat.sendComponent(player, timeago + " " + tag + " " + Phrase.build(Phrase.LOOKUP_CONTAINER, Color.DARK_AQUA + rbd + dplayer + Color.WHITE + rbd, "x" + amount, ChatUtils.createTooltip(Color.DARK_AQUA + rbd + dname, tooltip) + ChatUtils.createGiveItemComponent(Color.GREY + "(↓)", command.getName(), itemId) + Color.WHITE, selector));
PluginChannelListener.getInstance().sendData(player, Integer.parseInt(time), Phrase.LOOKUP_CONTAINER, selector, dplayer, dname, amount, dataX, dataY, dataZ, wid, rbd, true, tag.contains("+"));
}
}
else {
for (String[] data : lookupList) {
int drb = Integer.parseInt(data[8]);
String rbd = "";
if (drb == 1 || drb == 3) {
rbd = Color.STRIKETHROUGH;
}

String time = data[0];
String dplayer = data[1];
int dataX = Integer.parseInt(data[2]);
int dataY = Integer.parseInt(data[3]);
int dataZ = Integer.parseInt(data[4]);
int dtype = Integer.parseInt(data[5]);
int ddata = Integer.parseInt(data[6]);
int daction = Integer.parseInt(data[7]);
int wid = Integer.parseInt(data[9]);
int amount = Integer.parseInt(data[10]);
String tag = Color.WHITE + "-";

String timeago = ChatUtils.getTimeSince(Integer.parseInt(time), unixtimestamp, true);
int timeLength = 50 + (ChatUtils.getTimeSince(Integer.parseInt(time), unixtimestamp, false).replaceAll("[^0-9]", "").length() * 6);
String leftPadding = Color.BOLD + Strings.padStart("", 10, ' ');
if (timeLength % 4 == 0) {
leftPadding = Strings.padStart("", timeLength / 4, ' ');
}
else {
leftPadding = leftPadding + Color.WHITE + Strings.padStart("", (timeLength - 50) / 4, ' ');
}

String dname = "";
boolean isPlayer = false;
if (daction == 3 && !actions.contains(11) && amount == -1) {
if (dtype == 0) {
if (ConfigHandler.playerIdCacheReversed.get(ddata) == null) {
UserStatement.loadName(connection, ddata);
}
dname = ConfigHandler.playerIdCacheReversed.get(ddata);
isPlayer = true;
}
else {
dname = EntityUtils.getEntityType(dtype).name();
}
}
else {
dname = MaterialUtils.getType(dtype).name().toLowerCase(Locale.ROOT);
dname = StringUtils.nameFilter(dname, ddata);
}
if (dname.length() > 0 && !isPlayer) {
dname = "minecraft:" + dname.toLowerCase(Locale.ROOT) + "";
}

// Hide "minecraft:" for now.
if (dname.contains("minecraft:")) {
String[] blockNameSplit = dname.split(":");
dname = blockNameSplit[1];
}

// Functions.sendMessage(player2, timeago+" " + ChatColors.WHITE + "- " + ChatColors.DARK_AQUA+rbd+""+dplayer+" " + ChatColors.WHITE+rbd+""+a+" " + ChatColors.DARK_AQUA+rbd+"#"+dtype+ChatColors.WHITE + ". " + ChatColors.GREY + "(x"+x+"/y"+y+"/z"+z+")");

Phrase phrase = Phrase.LOOKUP_BLOCK;
String selector = Selector.FIRST;
String action = "a:block";
if (actions.contains(4) || actions.contains(5) || actions.contains(11) || amount > -1) {
byte[] metadata = data[11] == null ? null : data[11].getBytes(StandardCharsets.ISO_8859_1);
String tooltip = ItemUtils.getEnchantments(metadata, dtype, amount);
Integer itemId = ItemUtils.makeGivableItem(ItemUtils.getItemStack(metadata, dtype, amount));

if (daction == 2 || daction == 3) {
phrase = Phrase.LOOKUP_ITEM; // {picked up|dropped}
selector = (daction != 2 ? Selector.FIRST : Selector.SECOND);
tag = (daction != 2 ? Color.GREEN + "+" : Color.RED + "-");
action = "a:item";
}
else if (daction == 4 || daction == 5) {
phrase = Phrase.LOOKUP_STORAGE; // {deposited|withdrew}
selector = (daction != 4 ? Selector.FIRST : Selector.SECOND);
tag = (daction != 4 ? Color.RED + "-" : Color.GREEN + "+");
action = "a:item";
}
else if (daction == 6 || daction == 7) {
phrase = Phrase.LOOKUP_PROJECTILE; // {threw|shot}
selector = (daction != 7 ? Selector.FIRST : Selector.SECOND);
tag = Color.RED + "-";
action = "a:item";
}
else {
phrase = Phrase.LOOKUP_CONTAINER; // {added|removed}
selector = (daction != 0 ? Selector.FIRST : Selector.SECOND);
tag = (daction != 0 ? Color.GREEN + "+" : Color.RED + "-");
action = "a:container";
}

Chat.sendComponent(player, timeago + " " + tag + " " + Phrase.build(phrase, Color.DARK_AQUA + rbd + dplayer + Color.WHITE + rbd, "x" + amount, ChatUtils.createTooltip(Color.DARK_AQUA + rbd + dname, tooltip) + Color.WHITE, selector));
Chat.sendComponent(player, timeago + " " + tag + " " + Phrase.build(phrase, Color.DARK_AQUA + rbd + dplayer + Color.WHITE + rbd, "x" + amount, ChatUtils.createTooltip(Color.DARK_AQUA + rbd + dname, tooltip) + ChatUtils.createGiveItemComponent(Color.GREY + "(↓)", command.getName(), itemId) + Color.WHITE, selector));
PluginChannelListener.getInstance().sendData(player, Integer.parseInt(time), phrase, selector, dplayer, dname, (tag.contains("+") ? 1 : -1), dataX, dataY, dataZ, wid, rbd, action.contains("container"), tag.contains("+"));
}
else {
if (daction == 2 || daction == 3) {
phrase = Phrase.LOOKUP_INTERACTION; // {clicked|killed}
selector = (daction != 3 ? Selector.FIRST : Selector.SECOND);
tag = (daction != 3 ? Color.WHITE + "-" : Color.RED + "-");
action = (daction == 2 ? "a:click" : "a:kill");
}
else {
phrase = Phrase.LOOKUP_BLOCK; // {placed|broke}
selector = (daction != 0 ? Selector.FIRST : Selector.SECOND);
tag = (daction != 0 ? Color.GREEN + "+" : Color.RED + "-");
}

Chat.sendComponent(player, timeago + " " + tag + " " + Phrase.build(phrase, Color.DARK_AQUA + rbd + dplayer + Color.WHITE + rbd, Color.DARK_AQUA + rbd + dname + Color.WHITE, selector));
PluginChannelListener.getInstance().sendData(player, Integer.parseInt(time), phrase, selector, dplayer, dname, (tag.contains("+") ? 1 : -1), dataX, dataY, dataZ, wid, rbd, false, tag.contains("+"));
}

action = (actions.size() == 0 ? " (" + action + ")" : "");
Chat.sendComponent(player, Color.WHITE + leftPadding + Color.GREY + "^ " + ChatUtils.getCoordinates(command.getName(), wid, dataX, dataY, dataZ, true, true) + Color.GREY + Color.ITALIC + action);
}
}
if (rows > displayResults) {
int total_pages = (int) Math.ceil(rows / (displayResults + 0.0));
if (actions.contains(6) || actions.contains(7) || actions.contains(9) || (actions.contains(4) && actions.contains(11))) {
Chat.sendMessage(player, "-----");
}
Chat.sendComponent(player, ChatUtils.getPageNavigation(command.getName(), page, total_pages));
}
}
else if (rows > 0) {
Chat.sendMessage(player, Color.DARK_AQUA + "CoreProtect " + Color.WHITE + "- " + Phrase.build(Phrase.NO_RESULTS_PAGE, Selector.FIRST));
}
else {
Chat.sendMessage(player, Color.DARK_AQUA + "CoreProtect " + Color.WHITE + "- " + Phrase.build(Phrase.NO_RESULTS));
}
}
else {
Chat.sendMessage(player, Color.DARK_AQUA + "CoreProtect " + Color.WHITE + "- " + Phrase.build(Phrase.USER_NOT_FOUND, baduser));
}
statement.close();
}
else {
Chat.sendMessage(player, Color.DARK_AQUA + "CoreProtect " + Color.WHITE + "- " + Phrase.build(Phrase.DATABASE_BUSY));
}
}
catch (Exception e) {
e.printStackTrace();
}

ConfigHandler.lookupThrottle.put(player.getName(), new Object[] { false, System.currentTimeMillis() });
}

Check warning on line 471 in src/main/java/net/coreprotect/command/lookup/StandardLookupThread.java

View check run for this annotation

codefactor.io / CodeFactor

src/main/java/net/coreprotect/command/lookup/StandardLookupThread.java#L92-L471

Very Complex Method
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package net.coreprotect.command.parser;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class GivableItemIdParser {

protected static final Pattern PATTERN = Pattern.compile("#([0-9]+)");

public static Integer parseGivableItemId(String[] inputArguments) {
for (String argument : inputArguments) {
Matcher matcher = PATTERN.matcher(argument);
if (matcher.find()) {
return Integer.parseInt(matcher.group(1));
}
}
return null;
}
}
1 change: 1 addition & 0 deletions src/main/java/net/coreprotect/language/Language.java
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ public static void loadPhrases() {
phrases.put(Phrase.INVALID_DONATION_KEY, "Invalid donation key.");
phrases.put(Phrase.INVALID_INCLUDE, "\"{0}\" is an invalid block/entity name.");
phrases.put(Phrase.INVALID_INCLUDE_COMBO, "That is an invalid block/entity combination.");
phrases.put(Phrase.INVALID_ITEM_ID, "Please enter a valid item id.");
phrases.put(Phrase.INVALID_RADIUS, "Please enter a valid radius.");
phrases.put(Phrase.INVALID_SELECTION, "{0} selection not found.");
phrases.put(Phrase.INVALID_USERNAME, "\"{0}\" is an invalid username.");
Expand Down
1 change: 1 addition & 0 deletions src/main/java/net/coreprotect/language/Phrase.java
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ public enum Phrase {
INVALID_DONATION_KEY,
INVALID_INCLUDE,
INVALID_INCLUDE_COMBO,
INVALID_ITEM_ID,
INVALID_RADIUS,
INVALID_SELECTION,
INVALID_USERNAME,
Expand Down
8 changes: 8 additions & 0 deletions src/main/java/net/coreprotect/utility/ChatUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,14 @@ public static String createTooltip(String phrase, String tooltip) {
return message.append(Chat.COMPONENT_TAG_CLOSE).toString();
}

public static String createGiveItemComponent(String phrase, String command, Integer itemId) {
if (itemId == null) {
return "";
}

return Chat.COMPONENT_TAG_OPEN + Chat.COMPONENT_COMMAND + "|/" + command + " give #" + itemId + "|" + phrase + Chat.COMPONENT_TAG_CLOSE;
}

// This theoretically initializes the component code, to prevent gson adapter errors
public static void sendConsoleComponentStartup(ConsoleCommandSender consoleSender, String string) {
Chat.sendComponent(consoleSender, Color.RESET + "[CoreProtect] " + string + Chat.COMPONENT_TAG_OPEN + Chat.COMPONENT_POPUP + "| | " + Chat.COMPONENT_TAG_CLOSE);
Expand Down
30 changes: 26 additions & 4 deletions src/main/java/net/coreprotect/utility/ItemUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
Expand All @@ -30,11 +31,25 @@
import net.coreprotect.utility.serialize.ItemMetaHandler;

public class ItemUtils {
private static final Map<ItemStack, Integer> GIVABLE_ITEMS = Collections.synchronizedMap(new LinkedHashMap<>());

private ItemUtils() {
throw new IllegalStateException("Utility class");
}

public static ItemStack getGivableItem(int id) {
//we can use skip here because it's a linked map from which elements are never removed
return GIVABLE_ITEMS.keySet().stream().skip(id).findFirst().orElse(null);
}

public static Integer makeGivableItem(ItemStack item) {
if (item == null) {
return null;
}

return GIVABLE_ITEMS.computeIfAbsent(item, k -> GIVABLE_ITEMS.size());
}

public static void mergeItems(Material material, ItemStack[] items) {
if (material != null && (material.equals(Material.ARMOR_STAND) || BukkitAdapter.ADAPTER.isItemFrame(material))) {
return;
Expand Down Expand Up @@ -226,63 +241,63 @@
return contents;
}

public static ItemStack[] getContainerContents(Material type, Object container, Location location) {
ItemStack[] contents = null;
if (Config.getConfig(location.getWorld()).ITEM_TRANSACTIONS && BlockGroup.CONTAINERS.contains(type)) {
try {
// container may be null if called from within WorldEdit logger
if (container == null) {
container = location.getBlock();
}

if (type == Material.ARMOR_STAND) {
LivingEntity entity = (LivingEntity) container;
EntityEquipment equipment = getEntityEquipment(entity);
if (equipment != null) {
contents = getArmorStandContents(equipment);
}
}
else if (type == Material.ITEM_FRAME) {
ItemFrame entity = (ItemFrame) container;
contents = getItemFrameItem(entity);
}
else if (type == Material.JUKEBOX) {
org.bukkit.block.Jukebox blockState = (org.bukkit.block.Jukebox) ((Block) container).getState();
contents = BlockUtils.getJukeboxItem(blockState);
}
else {
Block block = (Block) container;
Inventory inventory = BlockUtils.getContainerInventory(block.getState(), true);
if (inventory != null) {
contents = inventory.getContents();
}
}

if (type == Material.ARMOR_STAND || type == Material.ITEM_FRAME) {
boolean hasItem = false;
for (ItemStack item : contents) {
if (item != null && !item.getType().equals(Material.AIR)) {
hasItem = true;
break;
}
}
if (!hasItem) {
contents = null;
}
}

if (contents != null) {
contents = getContainerState(contents);
}
}
catch (Exception e) {
e.printStackTrace();
}
}

return contents;
}

Check notice on line 300 in src/main/java/net/coreprotect/utility/ItemUtils.java

View check run for this annotation

codefactor.io / CodeFactor

src/main/java/net/coreprotect/utility/ItemUtils.java#L244-L300

Complex Method
public static EntityEquipment getEntityEquipment(LivingEntity entity) {
EntityEquipment equipment = null;
try {
Expand Down Expand Up @@ -372,14 +387,21 @@

return null;
}
public static String getEnchantments(byte[] metadata, int type, int amount) {

public static ItemStack getItemStack(byte[] metadata, int type, int amount) {
if (metadata == null) {
return "";
return null;
}

ItemStack item = new ItemStack(MaterialUtils.getType(type), amount);
item = (ItemStack) net.coreprotect.database.rollback.Rollback.populateItemStack(item, metadata)[2];
return item;
}

public static String getEnchantments(byte[] metadata, int type, int amount) {
var item = getItemStack(metadata, type, amount);
if (item == null) return "";

String displayName = item.hasItemMeta() && item.getItemMeta().hasDisplayName() ? item.getItemMeta().getDisplayName() : "";
StringBuilder message = new StringBuilder(Color.ITALIC + displayName + Color.GREY);

Expand All @@ -401,7 +423,7 @@

return message.toString();
}

public static Map<Integer, Object> serializeItemStackLegacy(ItemStack itemStack, String faceData, int slot) {
Map<Integer, Object> result = new HashMap<>();
Map<String, Object> itemMap = serializeItemStack(itemStack, faceData, slot);
Expand Down