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

Short term/code optimization #83

Merged
merged 15 commits into from
Jun 28, 2024
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
38 changes: 19 additions & 19 deletions .github/pull_request_template.md
Original file line number Diff line number Diff line change
@@ -1,40 +1,40 @@
# Pull Request

## Description
###### _- Please provide a brief description of the changes introduced by this pull request._

Please provide a brief description of the changes introduced by this pull request.

-
___
## Related Issues
###### _- List any related issues that this pull request addresses or resolves._

- List any related issues that this pull request addresses or resolves.

-
___
## Checklist

Please review and check the following before submitting your pull request:
### Please review and check the following before submitting your pull request:

- [ ] I have read and followed the [Contributing Guidelines](../CONTRIBUTING.md).
- [ ] My code follows the established [Code Style](../CONTRIBUTING.md#code-style).
- [ ] I have added appropriate comments and documentation.
- [ ] I have written unit tests for my changes (if applicable).
- [ ] My changes pass the continuous integration tests.
- [ ] I have reviewed and updated the documentation if necessary.
- [ ] I have checked for and resolved any merge conflicts.

- [ ] I have tested my changes locally.
- [ ] My code does not significantly impact the performance of any existing features in a negative way.
---
## Security
### Please ensure that your changes adhere to our [Security Policy](../SECURITY.md) and do not introduce security vulnerabilities.

Please ensure that your changes adhere to our [Security Policy](../SECURITY.md) and do not introduce security vulnerabilities.
- [ ] My code does not house any security vulnerabilities, backdoors or any other malicious code.
___
## Screenshots (if applicable, delete if not applicable)
###### _- Include any relevant screenshots or visual representations of your changes._

## Reviewers

Assign reviewers to your pull request.

## Screenshots (if applicable)

Include any relevant screenshots or visual representations of your changes.

___
## Additional Notes
###### _- Provide any additional information or context that reviewers or maintainers may find helpful._

Provide any additional information or context that reviewers or maintainers may find helpful.

## By submitting this pull request, I confirm that my contribution is made under the terms of the project's [LICENSE](../LICENSE.md).

___
## By submitting this pull request, I confirm that my contribution is made under the terms of the project's [LICENSE](../LICENSE.md).
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>5.10.2</version>
<version>5.10.3</version>
<scope>test</scope>
</dependency>

Expand All @@ -248,7 +248,7 @@
<dependency>
<groupId>de.tr7zw</groupId>
<artifactId>item-nbt-api</artifactId>
<version>2.12.4</version>
<version>2.13.1</version>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.gecko.spigotadmintoys.commands;

import org.bukkit.ChatColor;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
Expand Down Expand Up @@ -42,14 +43,17 @@ public boolean onCommand(CommandSender sender, Command cmd, String label, String
}

if (operation.equals("tnt")) {
int oldLimit = setAndGet.getTntRadiusLimit() - 2;
setAndGet.setTntRadiusLimit(newLimit); // Use the setter method for TNT operations
player.sendMessage("TNT radius set to " + newLimit);
player.sendMessage("TNT radius set from " + ChatColor.RED + oldLimit + ChatColor.RESET + " to " + ChatColor.GREEN + newLimit + newLimit);
} else if (operation.equals(PLAYER)) {
int oldLimit = setAndGet.getRadiusLimit() - 2;
setAndGet.setRadiusLimit(newLimit); // Use the setter method for player operations
player.sendMessage("Player operation limit set to " + newLimit);
player.sendMessage("Player operation limit set from " + ChatColor.RED + oldLimit + ChatColor.RESET + " to " + ChatColor.GREEN + newLimit);
} else {
setAndGet.setCreeperLimit(newLimit);
player.sendMessage("Creeper radius limit set to " + newLimit);
int oldLimit = setAndGet.getCreeperRadiusLimit() - 2;
setAndGet.setCreeperLimit(newLimit); // Use the setter method for creeper operations
player.sendMessage("Creeper radius limit set from" + ChatColor.RED + oldLimit + ChatColor.RESET + " to " + ChatColor.GREEN + newLimit);
}
} catch (NumberFormatException e) {
player.sendMessage("Please specify a valid integer.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ public class ConfigurationManager {
public static final String CREATED = "Config file created!";
private final File configFile;
private final Logger logger = Logger.getLogger(ConfigurationManager.class.getName());
private FileConfiguration config;
private final SetAndGet setAndGet;
private FileConfiguration config;

public ConfigurationManager(SetAndGet setAndGet) {
this.setAndGet = setAndGet;
Expand Down
11 changes: 6 additions & 5 deletions src/main/java/org/gecko/spigotadmintoys/gui/ConfigGUI.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.gecko.spigotadmintoys.gui;

import de.tr7zw.changeme.nbtapi.NBTItem;
import de.tr7zw.changeme.nbtapi.NBT;
import de.tr7zw.changeme.nbtapi.iface.ReadableItemNBT;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Material;
Expand All @@ -14,6 +15,8 @@
import org.gecko.spigotadmintoys.data.ConfigurationManager;
import org.gecko.spigotadmintoys.logic.SetAndGet;

import java.util.function.Function;

public class ConfigGUI implements Listener {

public static final String ENABLE_BUCKET = "Enable Bucket";
Expand Down Expand Up @@ -41,7 +44,6 @@ public class ConfigGUI implements Listener {
private int currentPage = 0;



public ConfigGUI(SetAndGet setAndGet) {
configManager = setAndGet.getConfigManager();
config = configManager.getConfig();
Expand Down Expand Up @@ -131,9 +133,8 @@ private boolean isClickEventValid(InventoryClickEvent event) {
}

private void handleItemClick(Player player, ItemStack clickedItem) {
if (clickedItem != null && (clickedItem.getType() == Material.INK_SACK || clickedItem.getType() == Material.PAPER || clickedItem.getType() == Material.CONCRETE ||clickedItem.getType() == Material.ARROW)) {
NBTItem nbtItem = new NBTItem(clickedItem);
String identifier = nbtItem.getString("Ident");
if (clickedItem != null && (clickedItem.getType() == Material.INK_SACK || clickedItem.getType() == Material.PAPER || clickedItem.getType() == Material.CONCRETE || clickedItem.getType() == Material.ARROW)) {
String identifier = NBT.get(clickedItem, (Function<ReadableItemNBT, String>) nbt -> nbt.getString("Ident"));
short data = clickedItem.getDurability();

if (handleButtonFeatures(player, identifier, data)) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package org.gecko.spigotadmintoys.gui.logic;

import de.tr7zw.changeme.nbtapi.NBTItem;
import de.tr7zw.changeme.nbtapi.NBT;
import de.tr7zw.changeme.nbtapi.iface.ReadWriteItemNBT;
import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;

import java.util.Collections;
import java.util.List;
import java.util.function.Consumer;

public class CreateButtonItem {

Expand All @@ -28,9 +30,8 @@ public ItemStack createButtonItem(Material material, String name, short data, St
meta.setLore(loreToString);
item.setItemMeta(meta);

NBTItem nbtItem = new NBTItem(item);
nbtItem.setString("Ident", ident);
NBT.modify(item, (Consumer<ReadWriteItemNBT>) nbt -> nbt.setString("Ident", ident));

return nbtItem.getItem();
return item;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,13 @@ public void assignPage(int page) {
this.playerLimit = setAndGet.getRadiusLimit() - 2;
this.creeperLimit = setAndGet.getCreeperRadiusLimit() - 2;
this.tntLimit = setAndGet.getTntRadiusLimit() - 2;
switch (page) {
case 0:
gui1();
break;
case 1:
gui2();
break;

if (page == 0) {
gui1();
} else if (page == 1) {
gui2();
}

gui.setItem(8, createButtonItem(Material.PAPER, ChatColor.RESET + "" + ChatColor.RED + "Reset config", (short) 0, null, "Reset"));
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package org.gecko.spigotadmintoys.items;

import de.tr7zw.changeme.nbtapi.NBTItem;
import de.tr7zw.changeme.nbtapi.NBT;
import de.tr7zw.changeme.nbtapi.iface.ReadWriteItemNBT;
import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;

import java.util.Collections;
import java.util.List;
import java.util.function.Consumer;

public class TriggerItems {

Expand All @@ -21,9 +23,8 @@ public ItemStack createCustomItem(Material material, String name, short data, St
meta.setLore(loreToList);
item.setItemMeta(meta);

NBTItem nbtItem = new NBTItem(item);
nbtItem.setString("Ident", ident);
NBT.modify(item, (Consumer<ReadWriteItemNBT>) nbt -> nbt.setString("Ident", ident));

return nbtItem.getItem();
return item;
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.gecko.spigotadmintoys.items.weapons;

import de.tr7zw.changeme.nbtapi.NBTItem;
import de.tr7zw.changeme.nbtapi.NBT;
import de.tr7zw.changeme.nbtapi.iface.ReadableItemNBT;
import org.bukkit.Material;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.entity.Arrow;
Expand All @@ -12,6 +13,8 @@
import org.gecko.spigotadmintoys.enchantments.enchants.weapons.bows.BowListener;
import org.gecko.spigotadmintoys.items.TriggerItems;

import java.util.function.Function;

public class Shortbow implements Listener {

public static final String SHORTBOWCONST = "Shortbow";
Expand All @@ -31,8 +34,7 @@ public void onPlayerBowClick(PlayerInteractEvent event) {
return;
}

NBTItem nbtItem = new NBTItem(event.getPlayer().getInventory().getItemInMainHand());
String identifier = nbtItem.getString("Ident");
String identifier = NBT.get(event.getPlayer().getInventory().getItemInMainHand(), (Function<ReadableItemNBT, String>) nbt -> nbt.getString("Ident"));

if (!identifier.equals(SHORTBOWCONST)) {
return;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.gecko.spigotadmintoys.listeners;

import de.tr7zw.changeme.nbtapi.NBTItem;
import de.tr7zw.changeme.nbtapi.NBT;
import de.tr7zw.changeme.nbtapi.iface.ReadableItemNBT;
import net.md_5.bungee.api.ChatMessageType;
import net.md_5.bungee.api.chat.TextComponent;
import org.bukkit.Bukkit;
Expand All @@ -15,10 +16,12 @@
import org.bukkit.event.block.BlockBreakEvent;
import org.bukkit.plugin.java.JavaPlugin;
import org.gecko.spigotadmintoys.Main;
import org.gecko.spigotadmintoys.logic.IterateBlocks;
import org.gecko.spigotadmintoys.logic.Scale;
import org.gecko.spigotadmintoys.logic.SetAndGet;

import java.util.*;
import java.util.function.Function;

public class BarrierListener implements Listener {

Expand Down Expand Up @@ -60,8 +63,7 @@ public void barrierBreakEventHandler(BlockBreakEvent event) {
BucketListener bucketListener = setAndGet.getBucketListener();
BedrockListener bedrockListener = setAndGet.getBedrockListener();
WaterBucketListener waterBucketListener = setAndGet.getWaterBucketListener();
NBTItem nbtItem = new NBTItem(event.getPlayer().getInventory().getItemInMainHand());
String identifier = nbtItem.getString("Ident");
String identifier = NBT.get(event.getPlayer().getInventory().getItemInMainHand(), (Function<ReadableItemNBT, String>) nbt -> nbt.getString("Ident"));
radiusLimit = setAndGet.getRadiusLimit();
realRadiusLimit = radiusLimit - 2;
showRemoval = setAndGet.getShowRemoval();
Expand Down Expand Up @@ -98,6 +100,7 @@ private void processBlockRemoval() {
Set<Block> nextSet = new HashSet<>();
boolean limitReachedThisIteration = false; // Variable to track whether the limit was reached this iteration
String bR = "Block removal: ";
IterateBlocks iterateBlocks = setAndGet.getIterateBlocks();
for (Block block : blocksToProcess) {
if (processedBlocks.contains(block)) {
continue;
Expand Down Expand Up @@ -138,7 +141,7 @@ private void processBlockRemoval() {
// Iterate through neighboring blocks and add them to the next set
for (int i = -1; i <= 1; i++) {
if (i == 0) continue; // Skip the current block
setAndGet.getIterateBlocks().iterateBlocks(block, nextSet, IMMUTABLE_MATERIALS, false);
iterateBlocks.iterateBlocks(block, nextSet, IMMUTABLE_MATERIALS, false);
}
processedBlocks.add(block);
}
Expand Down Expand Up @@ -183,19 +186,11 @@ public void displaySummary() {
String removed = "Removed ";
String dA = " dirt blocks and ";
String bB = " barrier blocks.";
if (barrierRemovedCount == 0 && grassRemovedCount == 0 && dirtRemovedCount > 0) {
player.sendMessage(ChatColor.GREEN + removed + ChatColor.RED + dirtRemovedCount + ChatColor.GREEN + " dirt blocks.");
} else if (barrierRemovedCount == 0 && dirtRemovedCount == 0 && grassRemovedCount > 0) {
player.sendMessage(ChatColor.GREEN + removed + ChatColor.RED + grassRemovedCount + ChatColor.GREEN + " grass blocks.");
} else if (barrierRemovedCount == 0 && grassRemovedCount > 0 && dirtRemovedCount > 0) {
player.sendMessage(ChatColor.GREEN + removed + ChatColor.RED + grassRemovedCount + ChatColor.GREEN + " grass blocks and " + ChatColor.RED + dirtRemovedCount + ChatColor.GREEN + " dirt blocks.");
} else if (barrierRemovedCount > 0 && grassRemovedCount > 0 && dirtRemovedCount > 0) {

if (barrierRemovedCount > 0 || grassRemovedCount > 0 || dirtRemovedCount > 0) {
player.sendMessage(ChatColor.GREEN + removed + ChatColor.RED + grassRemovedCount + ChatColor.GREEN + " grass blocks, " + ChatColor.RED + dirtRemovedCount + ChatColor.GREEN + dA + ChatColor.RED + barrierRemovedCount + ChatColor.GREEN + bB);
} else if (barrierRemovedCount > 0 && grassRemovedCount > 0 && dirtRemovedCount == 0) {
player.sendMessage(ChatColor.GREEN + removed + ChatColor.RED + grassRemovedCount + ChatColor.GREEN + " grass blocks and " + ChatColor.RED + barrierRemovedCount + ChatColor.GREEN + bB);
} else if (barrierRemovedCount > 0 && grassRemovedCount == 0 && dirtRemovedCount > 0) {
player.sendMessage(ChatColor.GREEN + removed + ChatColor.RED + dirtRemovedCount + ChatColor.GREEN + dA + ChatColor.RED + barrierRemovedCount + ChatColor.GREEN + bB);
}

// Display the block removal summary in the console
Bukkit.getConsoleSender().sendMessage(ChatColor.LIGHT_PURPLE + player.getName() + ChatColor.GREEN + " removed " + ChatColor.RED + grassRemovedCount + ChatColor.GREEN + " grass blocks, " + ChatColor.RED + dirtRemovedCount + ChatColor.GREEN + dA + ChatColor.RED + barrierRemovedCount + ChatColor.GREEN + bB);
if (!showRemoval) {
Expand All @@ -218,7 +213,7 @@ private void removeMarkedBlocks() {
}
cleanup();
} else {
scale.scaleReverseLogic(totalRemovedCount, radiusLimit, markedBlocks, "barrier");
scale.scaleReverseLogic(totalRemovedCount, radiusLimit, markedBlocks, "barrier", this::cleanRemove);

// If there are more blocks to remove, schedule the next batch
if (!markedBlocks.isEmpty()) {
Expand Down
Loading