Skip to content

Commit

Permalink
Fixes #102
Browse files Browse the repository at this point in the history
  • Loading branch information
sarhatabaot committed Nov 19, 2024
1 parent 64ae6b2 commit e8195a3
Show file tree
Hide file tree
Showing 10 changed files with 347 additions and 11 deletions.
2 changes: 2 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ dependencies {
implementation(libs.more.paper)
implementation(libs.annotations)
implementation(libs.xseries)
implementation(libs.boosted.yml)
}

bukkit {
Expand Down Expand Up @@ -106,6 +107,7 @@ tasks {
relocate("co.aikar.locales", "com.github.sarhatabaot.farmassistreboot.locales")
relocate("de.tr7zw.changeme.nbtapi", "com.github.sarhatabaot.farmassistreboot.nbt")
relocate("com.cryptomorin.xseries", "com.github.sarhatabaot.farmassistreboot.xseries")
relocate("dev.dejvokep.boostedyaml", "com.github.sarhatabaot.farmassistreboot.boostedyaml")
}
}

Expand Down
4 changes: 3 additions & 1 deletion build.gradle.kts~
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ plugins {
}

group = "com.github.sarhatabaot.farmassistreboot"
version = "1.4.6-BETA-2"
version = "1.4.6-BETA-3"

dependencies {
compileOnly(libs.spigot.api)
Expand All @@ -19,6 +19,7 @@ dependencies {
implementation(libs.more.paper)
implementation(libs.annotations)
implementation(libs.xseries)
implementation(libs.boosted.yml)
}

bukkit {
Expand Down Expand Up @@ -106,6 +107,7 @@ tasks {
relocate("co.aikar.locales", "com.github.sarhatabaot.farmassistreboot.locales")
relocate("de.tr7zw.changeme.nbtapi", "com.github.sarhatabaot.farmassistreboot.nbt")
relocate("com.cryptomorin.xseries", "com.github.sarhatabaot.farmassistreboot.xseries")
relocate("")
}
}

Expand Down
2 changes: 2 additions & 0 deletions settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ dependencyResolutionManagement {
version("plugin-yml", "0.6.0")
plugin("plugin-yml-bukkit", "net.minecrell.plugin-yml.bukkit").versionRef("plugin-yml")

library("boosted-yml", "dev.dejvokep:boosted-yaml:1.3.5")

plugin("shadow", "com.gradleup.shadow").version("8.3.0")

//Add our in-house messages plugin or find an alternative compat with java8
Expand Down
34 changes: 34 additions & 0 deletions settings.gradle.kts~
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
rootProject.name = "FarmAssistReboot"

dependencyResolutionManagement {
repositories {
mavenCentral()
maven("https://hub.spigotmc.org/nexus/content/repositories/snapshots/")
maven("https://repo.aikar.co/content/groups/aikar/")
maven("https://repo.codemc.org/repository/maven-public")
maven("https://repo.extendedclip.com/content/repositories/placeholderapi/")
maven("https://mvn-repo.arim.space/lesser-gpl3/")
maven("https://nexuslite.gcnt.net/repos/other/")
}

versionCatalogs {
create("libs") {
library("spigot-api", "org.spigotmc:spigot-api:1.13.2-R0.1-SNAPSHOT")
library("papi-api", "me.clip:placeholderapi:2.11.6")
library("acf-paper", "co.aikar:acf-paper:0.5.1-SNAPSHOT")
library("bstats", "org.bstats:bstats-bukkit:3.1.0")
library("nbt-api", "de.tr7zw:item-nbt-api:2.14.0")
library("annotations", "org.jetbrains:annotations:26.0.1")
library("more-paper", "space.arim.morepaperlib:morepaperlib:0.4.4")
library("xseries", "com.github.cryptomorin:XSeries:11.3.0")
version("plugin-yml", "0.6.0")
plugin("plugin-yml-bukkit", "net.minecrell.plugin-yml.bukkit").versionRef("plugin-yml")

library("boosted-yml", "dev.dejvokep:boosted-yaml")

plugin("shadow", "com.gradleup.shadow").version("8.3.0")

//Add our in-house messages plugin or find an alternative compat with java8
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,10 @@ public static boolean checkNoSeeds(final Player player) {
}

public static boolean checkSeedsOrNoSeedsInInventory(final Player player, final Material material) {
return !checkNoSeeds(player) || Util.inventoryContainsSeeds(player.getInventory(), material) == -1;
return checkSeedsOrNoSeedsInInventory(player, Util.inventoryContainsSeeds(player.getInventory(), material));
}

public static boolean checkSeedsOrNoSeedsInInventory(final Player player, int slot) {
return !checkNoSeeds(player) && slot == -1;
}
}
165 changes: 165 additions & 0 deletions src/main/java/com/github/sarhatabaot/farmassistreboot/Util.java~
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
package com.github.sarhatabaot.farmassistreboot;

import com.github.sarhatabaot.farmassistreboot.config.FarmAssistConfig;
import com.github.sarhatabaot.farmassistreboot.messages.Debug;
import com.github.sarhatabaot.farmassistreboot.messages.Permissions;
import com.github.sarhatabaot.farmassistreboot.tasks.ReplantTask;
import de.tr7zw.changeme.nbtapi.NBT;
import de.tr7zw.changeme.nbtapi.iface.ReadableItemNBT;
import me.clip.placeholderapi.PlaceholderAPI;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.block.Block;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.PlayerInventory;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


public class Util {
private static FarmAssistReboot plugin;

private Util() {
throw new UnsupportedOperationException();
}

public static void init(final FarmAssistReboot plugin) {
Util.plugin = plugin;
}

/**
* Will return -1 if no slot is found.
*
* @param playerInventory The player inventory
* @param material Material to check
* @return Return the slot matching the material. Will return -1 if no slot is found. Check for nbt/renamed.
*/
public static int inventoryContainsSeeds(@NotNull PlayerInventory playerInventory, @NotNull Material material) {
Crop crop = Crop.valueOf(material.name());
final Material seedMaterial = crop.getSeed().parseMaterial();
if (seedMaterial == null) {
debug("There was a problem parsing the crop or the seed material for: %s", material.name());
return -1;
}

Map<Integer, ? extends ItemStack> itemsSlotsMap = playerInventory.all(seedMaterial);
if (itemsSlotsMap.isEmpty())
return -1;
List<Map.Entry<Integer, ? extends ItemStack>> list = itemsSlotsMap.entrySet().stream()
.filter(p -> {
if (FarmAssistConfig.IGNORE_RENAMED) {
ItemStack itemStack = p.getValue();
return itemStack.getItemMeta() != null && !itemStack.getItemMeta().hasDisplayName();
}
return true;
})
.filter(p -> {
if (FarmAssistConfig.IGNORE_NBT) {
return NBT.get(p.getValue(), ReadableItemNBT::hasNBTData);
}
return true;
})
.collect(Collectors.toList());

if (list.isEmpty())
return -1;

return list.get(0).getKey();
}

public static boolean isWorldDisabled(@NotNull World world) {
debug(Debug.Worlds.CONFIG_PER_WORLD, FarmAssistConfig.ENABLED_PER_WORLD);
if (!FarmAssistConfig.ENABLED_PER_WORLD)
return false;

debug(Debug.Worlds.IS_WORLD_ENABLED, world.getName(), FarmAssistConfig.ENABLED_WORLDS.contains(world));
return !FarmAssistConfig.ENABLED_WORLDS.contains(world);
}

public static void replant(@NotNull Player player, Block block, @NotNull Material material) {
final Crop crop = Crop.valueOf(material.name());
final Material seedMaterial = crop.getSeed().parseMaterial();
if (seedMaterial == null) {
debug("There was a problem parsing the crop or the seed material for: %s", material.name());
return;
}

int spot = player.getInventory().first(seedMaterial);
replant(player, block, spot);
}

public static void replant(@NotNull Player player, Block block, int spot) {
debug("Spot:" + spot);
debug("CONFIG:no-seeds: %b, PERMISSION:farmassist.no_seeds: %b", FarmAssistConfig.NO_SEEDS, player.hasPermission(Permissions.NO_SEEDS));
if (spot >= 0 || Util.checkNoSeeds(player)) {
removeOrSubtractItem(player, spot);
plugin.getPaperLib().scheduling().regionSpecificScheduler(block.getLocation()).runDelayed(new ReplantTask(block,plugin), 5L);
}
}

public static void removeOrSubtractItem(@NotNull Player player, int spot) {
if(Util.checkNoSeeds(player)) {
return;
}

ItemStack next = player.getInventory().getItem(spot);
if (next != null && next.getAmount() > 1) {
next.setAmount(next.getAmount() - 1);
player.getInventory().setItem(spot, next);
} else {
player.getInventory().setItem(spot, new ItemStack(Material.AIR));
}
}

private static void debug(final String message) {
Util.plugin.debug(Util.class, message);
}

public static void debug(final String message, Object... args) {
debug(String.format(message, args));
}

public static void sendMessage(final @NotNull CommandSender sender, final String message) {
if (Bukkit.getPluginManager().getPlugin("PlaceholderAPI") != null) {
sendPrefixedAndColoredMessage(sender, PlaceholderAPI.setPlaceholders(!(sender instanceof Player) ? null : (Player) sender, message));
return;
}

sendPrefixedAndColoredMessage(sender, message);
}

public static void sendPrefixedAndColoredMessage(final @NotNull CommandSender sender, final String message) {
sender.sendMessage(color(plugin.getLanguageManager().getActiveLanguage().getPrefix() + message)); //Add this to lang manager
}

@Contract("_ -> new")
public static @NotNull String color(final String message) {
return ChatColor.translateAlternateColorCodes('&', message);
}

/**
* Checks if the "no-seeds" config option is enabled
* or if the player has the "no-seeds" permission.
* @param player Player to check
* @return true if the config option is enabled or if the player has the permission
*/
public static boolean checkNoSeeds(final Player player) {
return FarmAssistConfig.NO_SEEDS || player.hasPermission(Permissions.NO_SEEDS);
}

public static boolean checkSeedsOrNoSeedsInInventory(final Player player, final Material material) {
return checkSeedsOrNoSeedsInInventory(player, Util.inventoryContainsSeeds(player.getInventory(), material));
}

public static boolean checkSeedsOrNoSeedsInInventory(final Player player, int slot) {
return !checkNoSeeds(player) || slot == -1;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,9 @@ private void applyReplant(@NotNull BlockBreakEvent event) {
Material material = event.getBlock().getType();

int slot = Util.inventoryContainsSeeds(event.getPlayer().getInventory(), material);
if (Util.checkSeedsOrNoSeedsInInventory(event.getPlayer(),material)) {
if (Util.checkSeedsOrNoSeedsInInventory(event.getPlayer(), slot)) {
debug(Debug.OnBlockBreak.NO_SEEDS, event.getPlayer().getName());
debug("Material: %s, Seed: %s", material.name(), Crop.valueOf(material.name()).getSeed());
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public class BlockBreakListener implements Listener {
return;
}

if (!Util.isWorldEnabled(event.getPlayer().getWorld())) {
if (Util.isWorldDisabled(event.getPlayer().getWorld())) {
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,9 @@ public PlayerInteractionListener(FarmAssistReboot plugin) {
public void onPlayerInteract(PlayerInteractEvent event) {
if (!plugin.isGlobalEnabled()
|| !event.getAction().equals(Action.RIGHT_CLICK_BLOCK)
|| plugin.getDisabledPlayerList().contains(event.getPlayer().getUniqueId()))
|| plugin.getDisabledPlayerList().contains(event.getPlayer().getUniqueId())) {
return;
}

final Material itemInMainHandType = event.getPlayer().getInventory().getItemInMainHand().getType();
if (!(isHoe(itemInMainHandType) && isPlayerBlockFarmable(event))) {
Expand Down Expand Up @@ -60,14 +61,15 @@ public void onPlayerInteract(PlayerInteractEvent event) {
}

if (Util.checkSeedsOrNoSeedsInInventory(event.getPlayer(), Material.WHEAT)) {
final Block block = event.getClickedBlock();
if (block != null) {
event.getClickedBlock().setType(Material.FARMLAND);
}
return;
}

Util.replant(player, event.getClickedBlock(), Material.WHEAT);
final Block block = event.getClickedBlock();
if (block != null) {
event.getClickedBlock().setType(Material.FARMLAND);
}

Util.replant(player, event.getClickedBlock(), Material.WHEAT);
}

private boolean doesNotHaveWheatAndTillPermissions(final Player player) {
Expand All @@ -92,8 +94,9 @@ private boolean isHoe(@NotNull Material material) {
* @return true if block is farmable
*/
private boolean isPlayerBlockFarmable(@NotNull PlayerInteractEvent event) {
if(!event.hasBlock() || event.getClickedBlock() == null)
if (!event.hasBlock() || event.getClickedBlock() == null) {
return false;
}

final Block clickedBlock = event.getClickedBlock();

Expand Down
Loading

0 comments on commit e8195a3

Please sign in to comment.