9
0
mirror of https://github.com/Xiao-MoMi/Custom-Crops.git synced 2025-12-25 09:59:20 +00:00
This commit is contained in:
Xiao-MoMi
2023-01-18 02:14:51 +08:00
parent 14823c371f
commit 25e28963e6
7 changed files with 93 additions and 37 deletions

View File

@@ -17,26 +17,29 @@
package net.momirealms.customcrops.api.event;
import de.tr7zw.changeme.nbtapi.NBTItem;
import org.bukkit.Location;
import org.bukkit.entity.Player;
import org.bukkit.event.Cancellable;
import org.bukkit.event.HandlerList;
import org.bukkit.event.player.PlayerEvent;
import org.bukkit.inventory.ItemStack;
import org.jetbrains.annotations.NotNull;
public class WaterPotEvent extends PlayerEvent implements Cancellable {
private static final HandlerList handlers = new HandlerList();
private boolean cancelled;
private final NBTItem nbtItem;
private final ItemStack itemStack;
private int currentWater;
private final Location location;
public WaterPotEvent(@NotNull Player who, Location location, NBTItem nbtItem, int currentWater) {
/**
* ItemStack can be a water-bucket or a watering-can
*/
public WaterPotEvent(@NotNull Player who, Location location, ItemStack itemStack, int currentWater) {
super(who);
this.cancelled = false;
this.nbtItem = nbtItem;
this.itemStack = itemStack;
this.currentWater = currentWater;
this.location = location;
}
@@ -62,8 +65,8 @@ public class WaterPotEvent extends PlayerEvent implements Cancellable {
return getHandlerList();
}
public NBTItem getNbtItem() {
return nbtItem;
public ItemStack getItemStack() {
return itemStack;
}
public int getCurrentWater() {

View File

@@ -17,6 +17,7 @@
package net.momirealms.customcrops.integrations.customplugin;
import com.willfp.eco.core.items.Items;
import de.tr7zw.changeme.nbtapi.NBTCompound;
import de.tr7zw.changeme.nbtapi.NBTItem;
import net.kyori.adventure.text.minimessage.MiniMessage;
@@ -314,7 +315,6 @@ public abstract class HandlerP extends Function {
if (player.getGameMode() != GameMode.CREATIVE) item.setAmount(item.getAmount() - 1);
customWorld.addSprinklerCache(sprinklerLoc, sprinkler);
customInterface.placeFurniture(sprinklerLoc, config.getThreeD());
return true;
}
return false;
@@ -639,6 +639,20 @@ public abstract class HandlerP extends Function {
return true;
}
protected boolean useBucket(Location potLoc, Player player, ItemStack itemInHand) {
if (itemInHand.getType() == Material.WATER_BUCKET) {
WaterPotEvent waterPotEvent = new WaterPotEvent(player, potLoc, itemInHand, 0);
Bukkit.getPluginManager().callEvent(waterPotEvent);
if (waterPotEvent.isCancelled()) {
return false;
}
itemInHand.setType(Material.BUCKET);
waterPot(1,1, potLoc, 0);
return true;
}
return false;
}
protected boolean isInCoolDown(Player player, int delay) {
long time = System.currentTimeMillis();
if (time - (coolDown.getOrDefault(player, time - delay)) < delay) return true;

View File

@@ -125,8 +125,12 @@ public abstract class ItemsAdderHandler extends HandlerP {
public boolean tryMisc(Player player, ItemStack itemInHand, Location potLoc) {
if (!AntiGrief.testPlace(player, potLoc)) return true;
if (itemInHand == null || itemInHand.getType() == Material.AIR) return true;
CustomStack customStack = CustomStack.byItemStack(itemInHand);
if (useBucket(potLoc, player, itemInHand)) {
return true;
}
CustomStack customStack = CustomStack.byItemStack(itemInHand);
if (customStack == null) return false;
String itemID = customStack.getNamespacedID();
@@ -153,17 +157,17 @@ public abstract class ItemsAdderHandler extends HandlerP {
int water = nbtItem.getInteger("WaterAmount");
if (water > 0) {
WaterPotEvent waterPotEvent = new WaterPotEvent(player, potLoc, nbtItem, --water);
WaterPotEvent waterPotEvent = new WaterPotEvent(player, potLoc, itemStack, --water);
Bukkit.getPluginManager().callEvent(waterPotEvent);
if (waterPotEvent.isCancelled()) {
return true;
}
nbtItem = new NBTItem(waterPotEvent.getItemStack());
NBTCompound nbtCompound = nbtItem.getCompound("itemsadder");
if (nbtCompound.hasKey("custom_durability")){
if (nbtCompound.hasTag("custom_durability")){
int dur = nbtCompound.getInteger("custom_durability");
int max_dur = nbtCompound.getInteger("max_custom_durability");
if (dur > 0){
if (dur > 0) {
nbtCompound.setInteger("custom_durability", dur - 1);
nbtCompound.setDouble("fake_durability", (int) itemStack.getType().getMaxDurability() * (double) (dur/max_dur));
nbtItem.setInteger("Damage", (int) (itemStack.getType().getMaxDurability() * (1 - (double) dur/max_dur)));

View File

@@ -88,6 +88,11 @@ public abstract class OraxenHandler extends HandlerP {
public boolean tryMisc(Player player, ItemStack itemInHand, Location potLoc) {
if (!AntiGrief.testPlace(player, potLoc)) return true;
if (itemInHand == null || itemInHand.getType() == Material.AIR) return true;
if (useBucket(potLoc, player, itemInHand)) {
return true;
}
String id = OraxenItems.getIdByItem(itemInHand);
if (id == null) return false;
@@ -100,6 +105,7 @@ public abstract class OraxenHandler extends HandlerP {
if (useWateringCan(potLoc, id, player, itemInHand)) {
return true;
}
return false;
//for future misc
}
@@ -146,12 +152,13 @@ public abstract class OraxenHandler extends HandlerP {
int water = nbtItem.getInteger("WaterAmount");
if (water > 0) {
WaterPotEvent waterPotEvent = new WaterPotEvent(player, potLoc, nbtItem, --water);
WaterPotEvent waterPotEvent = new WaterPotEvent(player, potLoc, can, --water);
Bukkit.getPluginManager().callEvent(waterPotEvent);
if (waterPotEvent.isCancelled()) {
return true;
}
nbtItem = new NBTItem(waterPotEvent.getItemStack());
nbtItem.setInteger("WaterAmount", waterPotEvent.getCurrentWater());
if (SoundConfig.waterPot.isEnable()) {

View File

@@ -17,26 +17,42 @@
package net.momirealms.customcrops.integrations.protection;
import me.angeschossen.lands.api.LandsIntegration;
import me.angeschossen.lands.api.flags.Flags;
import me.angeschossen.lands.api.flags.enums.FlagTarget;
import me.angeschossen.lands.api.flags.enums.RoleFlagCategory;
import me.angeschossen.lands.api.flags.types.RoleFlag;
import me.angeschossen.lands.api.land.Area;
import me.angeschossen.lands.api.land.LandWorld;
import net.momirealms.customcrops.CustomCrops;
import net.momirealms.customcrops.integrations.AntiGrief;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.entity.Player;
public class LandsHook implements AntiGrief {
private final LandsIntegration api;
public LandsHook() {
api = LandsIntegration.of(CustomCrops.plugin);;
}
@Override
public boolean canBreak(Location location, Player player) {
Area area = new me.angeschossen.lands.api.integration.LandsIntegration(CustomCrops.plugin).getAreaByLoc(location);
if (area != null) return area.hasFlag(player, Flags.BLOCK_BREAK, false);
else return true;
LandWorld world = api.getWorld(location.getWorld());
if (world != null) {
return world.hasRoleFlag(player.getUniqueId(), location, RoleFlag.of("BLOCK_BREAK"));
}
return true;
}
@Override
public boolean canPlace(Location location, Player player) {
Area area = new me.angeschossen.lands.api.integration.LandsIntegration(CustomCrops.plugin).getAreaByLoc(location);
if (area != null) return area.hasFlag(player, Flags.BLOCK_PLACE, false);
else return true;
LandWorld world = api.getWorld(location.getWorld());
if (world != null) {
return world.hasRoleFlag(player.getUniqueId(), location, RoleFlag.of("BLOCK_PLACE"));
}
return true;
}
}

View File

@@ -64,7 +64,6 @@ public class CustomWorld {
private int timer;
public CustomWorld(World world, CropManager cropManager) {
this.world = world;
this.fertilizerCache = new ConcurrentHashMap<>(2048);
this.sprinklerCache = new ConcurrentHashMap<>(512);
@@ -370,10 +369,14 @@ public class CustomWorld {
}
else if (!compensation) {
route(sprinklerTime);
potDryJudge(sprinklerTime + randomGenerator.nextInt(dryTime));
for (Map.Entry<SimpleLocation, GrowingCrop> entry : cropData.entrySet()) {
growSingleWire(entry.getKey(), entry.getValue(), sprinklerTime + dryTime + randomGenerator.nextInt(cropTime));
}
Bukkit.getScheduler().runTaskLaterAsynchronously(CustomCrops.plugin, () -> {
potDryJudge(dryTime);
}, sprinklerTime);
}
else {
int delay = (int) (24000 - world.getTime());
@@ -405,10 +408,14 @@ public class CustomWorld {
}
else if (!compensation) {
route(sprinklerTime);
potDryJudge(sprinklerTime + randomGenerator.nextInt(dryTime));
for (Map.Entry<SimpleLocation, GrowingCrop> entry : cropData.entrySet()) {
growSingleFrame(entry.getKey(), entry.getValue(), sprinklerTime + dryTime + randomGenerator.nextInt(cropTime));
}
Bukkit.getScheduler().runTaskLaterAsynchronously(CustomCrops.plugin, () -> {
potDryJudge(dryTime);
}, sprinklerTime);
}
else {
int delay = (int) (24000 - world.getTime());
@@ -438,13 +445,18 @@ public class CustomWorld {
private void route(int sprinklerTime) {
//先将湿润的种植盆放入等待判断的种植盆列表中
tempWatered = new HashSet<>(watered);
//清空湿润的
watered.clear();
//玩家浇水
watered.addAll(playerWatered);
//清除昨日玩家浇水
playerWatered.clear();
//清除玩家昨日种植
plantedToday.clear();;
//洒水器工作会把种植盆放入watered中
Random randomGenerator = new Random();
for (Map.Entry<SimpleLocation, Sprinkler> sprinklerEntry : sprinklerCache.entrySet()) {
bukkitScheduler.runTaskLaterAsynchronously(CustomCrops.plugin, () -> sprinklerWork(sprinklerEntry.getKey(), sprinklerEntry.getValue()), randomGenerator.nextInt(sprinklerTime));
@@ -489,25 +501,25 @@ public class CustomWorld {
}
}
private void potDryJudge(int time) {
new BukkitRunnable() {
@Override
public void run() {
tempWatered.removeAll(watered);
for (SimpleLocation simpleLocation : tempWatered) {
private void potDryJudge(int dry_time) {
//将待干的种植盆中今日被浇水的种植盆移除
//剩余的种植盆即为需要变干的
tempWatered.removeAll(watered);
for (SimpleLocation simpleLocation : tempWatered) {
new BukkitRunnable() {
@Override
public void run() {
Location potLoc = MiscUtils.getLocation(simpleLocation);
if (potLoc == null) return;
if (!isPotWet(potLoc)) {
cropManager.makePotDry(potLoc);
continue;
}
Fertilizer fertilizer = getFertilizerCache(potLoc);
if (!(fertilizer instanceof RetainingSoil retainingSoil && Math.random() < retainingSoil.getChance())) {
cropManager.makePotDry(potLoc);
}
}
}
}.runTaskLaterAsynchronously(CustomCrops.plugin, time);
}.runTaskLaterAsynchronously(CustomCrops.plugin, new Random().nextInt(dry_time));
}
//用完就抛弃
tempWatered.clear();
}
/**