9
0
mirror of https://github.com/Xiao-MoMi/Custom-Crops.git synced 2025-12-22 16:39:36 +00:00
This commit is contained in:
XiaoMoMi
2024-01-04 18:00:50 +08:00
parent 73b090ec40
commit c6cf8bd5b7
11 changed files with 88 additions and 26 deletions

View File

@@ -21,10 +21,8 @@ import net.momirealms.customcrops.api.object.CCGrowingCrop;
import net.momirealms.customcrops.api.object.CCPot;
import net.momirealms.customcrops.api.object.CCSprinkler;
import net.momirealms.customcrops.api.object.CCWorldSeason;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.plugin.Plugin;
import javax.annotation.Nullable;

View File

@@ -8,7 +8,7 @@ plugins {
allprojects {
project.group = "net.momirealms"
project.version = "3.3.1.9"
project.version = "3.3.1.10"
apply<JavaPlugin>()
apply(plugin = "java")

View File

@@ -22,11 +22,9 @@ import com.comphenix.protocol.ProtocolManager;
import net.kyori.adventure.platform.bukkit.BukkitAudiences;
import net.momirealms.customcrops.api.CustomCropsAPIImpl;
import net.momirealms.customcrops.api.CustomCropsPlugin;
import net.momirealms.customcrops.api.event.CropBreakEvent;
import net.momirealms.customcrops.api.object.basic.ConfigManager;
import net.momirealms.customcrops.api.object.basic.MessageManager;
import net.momirealms.customcrops.api.object.crop.CropManager;
import net.momirealms.customcrops.api.object.crop.GrowingCrop;
import net.momirealms.customcrops.api.object.fertilizer.FertilizerManager;
import net.momirealms.customcrops.api.object.hologram.HologramManager;
import net.momirealms.customcrops.api.object.pot.PotManager;
@@ -34,7 +32,6 @@ import net.momirealms.customcrops.api.object.scheduler.Scheduler;
import net.momirealms.customcrops.api.object.season.SeasonManager;
import net.momirealms.customcrops.api.object.sprinkler.SprinklerManager;
import net.momirealms.customcrops.api.object.wateringcan.WateringCanManager;
import net.momirealms.customcrops.api.object.world.SimpleLocation;
import net.momirealms.customcrops.api.object.world.WorldDataManager;
import net.momirealms.customcrops.command.CustomCropsCommand;
import net.momirealms.customcrops.customplugin.Platform;

View File

@@ -24,19 +24,19 @@ import net.momirealms.customcrops.api.object.world.SimpleLocation;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.Nullable;
public record DropItemImpl(Loot[] loots) implements Action {
public record DropItemImpl(Loot[] loots, boolean toInv) implements Action {
@Override
public void doOn(@Nullable Player player, @Nullable SimpleLocation cropLoc, ItemMode itemMode) {
if (cropLoc == null) return;
if (player != null) {
for (Loot loot : loots) {
loot.drop(player, cropLoc.getBukkitLocation());
loot.drop(player, cropLoc.getBukkitLocation(), toInv);
}
} else {
CustomCrops.getInstance().getScheduler().runTask(() -> {
for (Loot loot : loots) {
loot.drop(null, cropLoc.getBukkitLocation());
loot.drop(null, cropLoc.getBukkitLocation(), toInv);
}
});
}

View File

@@ -38,9 +38,7 @@ public abstract class Loot {
this.max = max;
}
public void drop(@Nullable Player player, Location location) {
//empty
}
public abstract void drop(@Nullable Player player, Location location, boolean toInv);
public int getMin() {
return min;

View File

@@ -21,6 +21,7 @@ import net.momirealms.customcrops.CustomCrops;
import net.momirealms.customcrops.api.object.fertilizer.YieldIncrease;
import net.momirealms.customcrops.api.object.pot.Pot;
import net.momirealms.customcrops.api.object.world.SimpleLocation;
import net.momirealms.customcrops.util.ItemUtils;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.entity.Player;
@@ -45,7 +46,8 @@ public class OtherLoot extends Loot {
return chance;
}
public void drop(Player player, Location location) {
@Override
public void drop(Player player, Location location, boolean toInv) {
if (Math.random() < getChance()) {
int random = getAmount(player);
Pot pot = CustomCrops.getInstance().getWorldDataManager().getPotData(SimpleLocation.getByBukkitLocation(location).add(0,-1,0));
@@ -55,7 +57,16 @@ public class OtherLoot extends Loot {
ItemStack drop = CustomCrops.getInstance().getIntegrationManager().build(getItemID(), player);
if (drop.getType() == Material.AIR) return;
drop.setAmount(random);
location.getWorld().dropItemNaturally(location, drop);
if (toInv) {
int remain = ItemUtils.putLootsToBag(player.getInventory(), drop, drop.getAmount());
if (remain > 0) {
drop.setAmount(remain);
location.getWorld().dropItemNaturally(location, drop);
}
} else {
location.getWorld().dropItemNaturally(location, drop);
}
}
}
}

View File

@@ -24,6 +24,7 @@ import net.momirealms.customcrops.api.object.fertilizer.Quality;
import net.momirealms.customcrops.api.object.fertilizer.YieldIncrease;
import net.momirealms.customcrops.api.object.pot.Pot;
import net.momirealms.customcrops.api.object.world.SimpleLocation;
import net.momirealms.customcrops.util.ItemUtils;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.entity.Player;
@@ -39,7 +40,7 @@ public class QualityLoot extends Loot {
}
@Override
public void drop(Player player, Location location) {
public void drop(Player player, Location location, boolean toInv) {
SimpleLocation simpleLocation = SimpleLocation.getByBukkitLocation(location);
Pot pot = CustomCrops.getInstance().getWorldDataManager().getPotData(simpleLocation.add(0,-1,0));
int amount = getAmount(player);
@@ -56,16 +57,24 @@ public class QualityLoot extends Loot {
double random = Math.random();
for (int j = 0; j < qualityRatio.length; j++) {
if (random < qualityRatio[j]) {
dropItem(location, qualityLoots[j], player);
dropItem(location, qualityLoots[j], player, toInv);
break;
}
}
}
}
private void dropItem(Location location, String id, Player player) {
private void dropItem(Location location, String id, Player player, boolean toInv) {
ItemStack drop = CustomCrops.getInstance().getIntegrationManager().build(id, player);
if (drop.getType() == Material.AIR) return;
location.getWorld().dropItemNaturally(location, drop);
if (toInv) {
int remain = ItemUtils.putLootsToBag(player.getInventory(), drop, drop.getAmount());
if (remain > 0) {
drop.setAmount(remain);
location.getWorld().dropItemNaturally(location, drop);
}
} else {
location.getWorld().dropItemNaturally(location, drop);
}
}
}

View File

@@ -18,7 +18,6 @@
package net.momirealms.customcrops.integration.skill;
import com.willfp.ecoskills.api.EcoSkillsAPI;
import com.willfp.ecoskills.skills.Skill;
import com.willfp.ecoskills.skills.Skills;
import net.momirealms.customcrops.integration.SkillInterface;
import org.bukkit.entity.Player;

View File

@@ -157,12 +157,12 @@ public class AdventureUtils {
case 'd' -> stringBuilder.append("<light_purple>");
case 'e' -> stringBuilder.append("<yellow>");
case 'f' -> stringBuilder.append("<white>");
case 'r' -> stringBuilder.append("<reset><!italic>");
case 'l' -> stringBuilder.append("<bold>");
case 'm' -> stringBuilder.append("<strikethrough>");
case 'o' -> stringBuilder.append("<italic>");
case 'n' -> stringBuilder.append("<underlined>");
case 'k' -> stringBuilder.append("<obfuscated>");
case 'r' -> stringBuilder.append("<reset>");
case 'l' -> stringBuilder.append("<b>");
case 'm' -> stringBuilder.append("<st>");
case 'o' -> stringBuilder.append("<i>");
case 'n' -> stringBuilder.append("<u>");
case 'k' -> stringBuilder.append("<obf>");
case 'x' -> {
if (i + 13 >= chars.length
|| !isColorCode(chars[i+2])

View File

@@ -302,6 +302,7 @@ public class ConfigUtils {
case "drop-items" -> {
ConfigurationSection lootSec = actionSec.getConfigurationSection("value");
if (lootSec == null) continue;
boolean directToInv = lootSec.getBoolean("to-inventory", false);
ArrayList<Loot> loots = new ArrayList<>();
if (lootSec.contains("quality-crops")) {
String[] qualityLoots = new String[ConfigManager.defaultRatio.length];
@@ -330,7 +331,7 @@ public class ConfigUtils {
loots.add(otherLoot);
}
}
actions.add(new DropItemImpl(loots.toArray(new Loot[0])));
actions.add(new DropItemImpl(loots.toArray(new Loot[0]), directToInv));
}
case "break" -> actions.add(new BreakImpl(
actionSec.getBoolean("value", true),

View File

@@ -0,0 +1,49 @@
package net.momirealms.customcrops.util;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
public class ItemUtils {
public static int putLootsToBag(Inventory inventory, ItemStack itemStack, int amount) {
ItemMeta meta = itemStack.getItemMeta();
int maxStackSize = itemStack.getMaxStackSize();
for (ItemStack other : inventory.getStorageContents()) {
if (other != null) {
if (other.getType() == itemStack.getType() && other.getItemMeta().equals(meta)) {
if (other.getAmount() < maxStackSize) {
int delta = maxStackSize - other.getAmount();
if (amount > delta) {
other.setAmount(maxStackSize);
amount -= delta;
} else {
other.setAmount(amount + other.getAmount());
return 0;
}
}
}
}
}
if (amount > 0) {
for (ItemStack other : inventory.getStorageContents()) {
if (other == null) {
if (amount > maxStackSize) {
amount -= maxStackSize;
ItemStack cloned = itemStack.clone();
cloned.setAmount(maxStackSize);
inventory.addItem(cloned);
} else {
ItemStack cloned = itemStack.clone();
cloned.setAmount(amount);
inventory.addItem(cloned);
return 0;
}
}
}
}
return amount;
}
}