9
0
mirror of https://github.com/Xiao-MoMi/Custom-Crops.git synced 2025-12-25 18:09:28 +00:00

Fix some logics

This commit is contained in:
XiaoMoMi
2024-09-01 16:39:25 +08:00
parent 139cd9649d
commit 37688940ca
36 changed files with 248 additions and 174 deletions

View File

@@ -24,6 +24,7 @@ import net.momirealms.customcrops.api.core.block.*;
import net.momirealms.customcrops.api.core.item.*;
import net.momirealms.customcrops.api.core.world.CustomCropsBlockState;
import net.momirealms.customcrops.api.event.CustomCropsReloadEvent;
import net.momirealms.customcrops.api.misc.HologramManager;
import net.momirealms.customcrops.api.misc.cooldown.CoolDownManager;
import net.momirealms.customcrops.api.misc.placeholder.BukkitPlaceholderManager;
import net.momirealms.customcrops.api.util.EventUtils;
@@ -33,7 +34,6 @@ import net.momirealms.customcrops.bukkit.command.BukkitCommandManager;
import net.momirealms.customcrops.bukkit.config.BukkitConfigManager;
import net.momirealms.customcrops.bukkit.integration.BukkitIntegrationManager;
import net.momirealms.customcrops.bukkit.item.BukkitItemManager;
import net.momirealms.customcrops.bukkit.misc.HologramManager;
import net.momirealms.customcrops.bukkit.requirement.BlockRequirementManager;
import net.momirealms.customcrops.bukkit.requirement.PlayerRequirementManager;
import net.momirealms.customcrops.bukkit.scheduler.BukkitSchedulerAdapter;
@@ -166,8 +166,9 @@ public class BukkitCustomCropsPluginImpl extends BukkitCustomCropsPlugin {
boolean downloadFromBBB = buildByBit.equals("true");
this.getScheduler().sync().runLater(() -> {
this.reload();
getPluginLogger().info("CustomCrops Registry has been frozen");
((SimpleRegistryAccess) registryAccess).freeze();
this.reload();
if (ConfigManager.metrics()) new Metrics((JavaPlugin) getBoostrap(), 16593);
if (ConfigManager.checkUpdate()) {
VersionHelper.UPDATE_CHECKER.apply(this).thenAccept(result -> {

View File

@@ -26,19 +26,16 @@ import net.momirealms.customcrops.api.BukkitCustomCropsPlugin;
import net.momirealms.customcrops.api.action.AbstractActionManager;
import net.momirealms.customcrops.api.action.Action;
import net.momirealms.customcrops.api.context.ContextKeys;
import net.momirealms.customcrops.api.core.block.CropBlock;
import net.momirealms.customcrops.api.core.block.CropConfig;
import net.momirealms.customcrops.api.core.block.CropStageConfig;
import net.momirealms.customcrops.api.core.world.CustomCropsBlockState;
import net.momirealms.customcrops.api.core.block.CustomCropsBlock;
import net.momirealms.customcrops.api.core.block.SprinklerBlock;
import net.momirealms.customcrops.api.core.block.SprinklerConfig;
import net.momirealms.customcrops.api.core.world.CustomCropsWorld;
import net.momirealms.customcrops.api.core.world.Pos3;
import net.momirealms.customcrops.api.misc.placeholder.BukkitPlaceholderManager;
import net.momirealms.customcrops.api.misc.value.MathValue;
import net.momirealms.customcrops.api.misc.value.TextValue;
import net.momirealms.customcrops.api.util.LocationUtils;
import net.momirealms.customcrops.api.util.PlayerUtils;
import net.momirealms.customcrops.bukkit.integration.VaultHook;
import net.momirealms.customcrops.bukkit.misc.HologramManager;
import net.momirealms.customcrops.common.helper.AdventureHelper;
import net.momirealms.customcrops.common.util.ListUtils;
import net.momirealms.customcrops.common.util.RandomUtils;
@@ -53,7 +50,10 @@ import org.bukkit.inventory.ItemStack;
import org.bukkit.potion.PotionEffect;
import org.bukkit.potion.PotionEffectType;
import java.util.*;
import java.util.List;
import java.util.Locale;
import java.util.Objects;
import java.util.Optional;
import static java.util.Objects.requireNonNull;
@@ -85,7 +85,6 @@ public class PlayerActionManager extends AbstractActionManager<Player> {
this.registerTitleAction();
this.registerSwingHandAction();
this.registerForceTickAction();
this.registerHologramAction();
this.registerMessageAction();
}
@@ -447,80 +446,19 @@ public class PlayerActionManager extends AbstractActionManager<Player> {
}, "random-title");
}
private void registerHologramAction() {
registerAction(((args, chance) -> {
if (args instanceof Section section) {
TextValue<Player> text = TextValue.auto(section.getString("text", ""));
MathValue<Player> duration = MathValue.auto(section.get("duration", 20));
boolean position = section.getString("position", "other").equals("other");
MathValue<Player> x = MathValue.auto(section.get("x", 0));
MathValue<Player> y = MathValue.auto(section.get("y", 0));
MathValue<Player> z = MathValue.auto(section.get("z", 0));
boolean applyCorrection = section.getBoolean("apply-correction", false);
boolean onlyShowToOne = !section.getBoolean("visible-to-all", false);
int range = section.getInt("range", 32);
return context -> {
if (context.holder() == null) return;
if (Math.random() > chance) return;
Player owner = context.holder();
Location location = position ? requireNonNull(context.arg(ContextKeys.LOCATION)).clone() : owner.getLocation().clone();
location.add(x.evaluate(context), y.evaluate(context), z.evaluate(context));
Optional<CustomCropsWorld<?>> optionalWorld = plugin.getWorldManager().getWorld(location.getWorld());
if (optionalWorld.isEmpty()) {
return;
}
Pos3 pos3 = Pos3.from(location);
if (applyCorrection) {
Optional<CustomCropsBlockState> optionalState = optionalWorld.get().getBlockState(pos3);
if (optionalState.isPresent()) {
if (optionalState.get().type() instanceof CropBlock cropBlock) {
CropConfig config = cropBlock.config(optionalState.get());
int point = cropBlock.point(optionalState.get());
if (config != null) {
int tempPoints = point;
while (tempPoints >= 0) {
Map.Entry<Integer, CropStageConfig> entry = config.getFloorStageEntry(tempPoints);
CropStageConfig stage = entry.getValue();
if (stage.stageID() != null) {
location.add(0, stage.displayInfoOffset(), 0);
break;
}
tempPoints = stage.point() - 1;
}
}
}
}
}
ArrayList<Player> viewers = new ArrayList<>();
if (onlyShowToOne) {
if (owner == null) return;
viewers.add(owner);
} else {
for (Player player : owner.getWorld().getPlayers()) {
if (LocationUtils.getDistance(player.getLocation(), location) <= range) {
viewers.add(player);
}
}
}
Component component = AdventureHelper.miniMessage(text.render(context));
for (Player viewer : viewers) {
HologramManager.getInstance().showHologram(viewer, location, component, (int) (duration.evaluate(context) * 50));
}
};
} else {
plugin.getPluginLogger().warn("Invalid value type: " + args.getClass().getSimpleName() + " found at hologram action which is expected to be `Section`");
return Action.empty();
}
}), "hologram");
}
private void registerSwingHandAction() {
registerAction((args, chance) -> {
boolean arg = (boolean) args;
return context -> {
if (context.holder() == null) return;
if (Math.random() > chance) return;
SparrowHeart.getInstance().swingHand(context.holder(), arg ? HandSlot.MAIN : HandSlot.OFF);
EquipmentSlot slot = context.arg(ContextKeys.SLOT);
if (slot == null) {
SparrowHeart.getInstance().swingHand(context.holder(), arg ? HandSlot.MAIN : HandSlot.OFF);
} else {
if (slot == EquipmentSlot.HAND) SparrowHeart.getInstance().swingHand(context.holder(), HandSlot.MAIN);
if (slot == EquipmentSlot.OFF_HAND) SparrowHeart.getInstance().swingHand(context.holder(), HandSlot.OFF);
}
};
}, "swing-hand");
}
@@ -533,9 +471,28 @@ public class PlayerActionManager extends AbstractActionManager<Player> {
Pos3 pos3 = Pos3.from(location);
Optional<CustomCropsWorld<?>> optionalWorld = plugin.getWorldManager().getWorld(location.getWorld());
optionalWorld.ifPresent(world -> world.getChunk(pos3.toChunkPos()).flatMap(chunk -> chunk.getBlockState(pos3)).ifPresent(state -> {
state.type().randomTick(state, world, pos3);
state.type().scheduledTick(state, world, pos3);
CustomCropsBlock customCropsBlock = state.type();
customCropsBlock.randomTick(state, world, pos3);
customCropsBlock.scheduledTick(state, world, pos3);
if (customCropsBlock instanceof SprinklerBlock sprinklerBlock) {
int water = sprinklerBlock.water(state);
SprinklerConfig config = sprinklerBlock.config(state);
context.arg(ContextKeys.CURRENT_WATER, water);
context.arg(ContextKeys.WATER_BAR, Optional.ofNullable(config.waterBar()).map(it -> it.getWaterBar(water, config.storage())).orElse(""));
}
}));
}, "force-tick");
registerAction((args, chance) -> context -> {
if (context.holder() == null) return;
if (Math.random() > chance) return;
Location location = requireNonNull(context.arg(ContextKeys.LOCATION));
Pos3 pos3 = Pos3.from(location);
Optional<CustomCropsWorld<?>> optionalWorld = plugin.getWorldManager().getWorld(location.getWorld());
optionalWorld.ifPresent(world -> world.getChunk(pos3.toChunkPos()).flatMap(chunk -> chunk.getBlockState(pos3)).ifPresent(state -> {
CustomCropsBlock customCropsBlock = state.type();
customCropsBlock.randomTick(state, world, pos3);
customCropsBlock.scheduledTick(state, world, pos3);
}));
}, "tick");
}
}

View File

@@ -38,15 +38,14 @@ import net.momirealms.customcrops.api.core.item.FertilizerConfig;
import net.momirealms.customcrops.api.core.item.WateringCanConfig;
import net.momirealms.customcrops.api.util.PluginUtils;
import net.momirealms.customcrops.common.helper.AdventureHelper;
import net.momirealms.customcrops.common.helper.VersionHelper;
import net.momirealms.customcrops.common.locale.TranslationManager;
import net.momirealms.customcrops.common.plugin.CustomCropsProperties;
import net.momirealms.customcrops.common.util.ListUtils;
import org.bukkit.Bukkit;
import org.bukkit.Particle;
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.*;
public class BukkitConfigManager extends ConfigManager {

View File

@@ -33,8 +33,8 @@ import net.momirealms.customcrops.api.core.item.FertilizerConfig;
import net.momirealms.customcrops.api.core.item.FertilizerType;
import net.momirealms.customcrops.api.core.item.WateringCanConfig;
import net.momirealms.customcrops.api.core.world.CustomCropsBlockState;
import net.momirealms.customcrops.api.misc.water.WaterBar;
import net.momirealms.customcrops.api.misc.value.TextValue;
import net.momirealms.customcrops.api.misc.water.WaterBar;
import net.momirealms.customcrops.api.requirement.RequirementManager;
import net.momirealms.customcrops.common.util.Pair;
import net.momirealms.customcrops.common.util.TriFunction;

View File

@@ -197,6 +197,7 @@ public class BukkitItemManager extends AbstractItemManager {
@Override
public void placeFurniture(@NotNull Location location, @NotNull String id, FurnitureRotation rotation) {
Entity entity = this.provider.placeFurniture(location, id);
if (rotation == FurnitureRotation.NONE) return;
if (entity != null) {
if (entity instanceof ItemFrame itemFrame) {
itemFrame.setRotation(rotation.getBukkitRotation());

View File

@@ -1,172 +0,0 @@
/*
* Copyright (C) <2024> <XiaoMoMi>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package net.momirealms.customcrops.bukkit.misc;
import net.kyori.adventure.text.Component;
import net.momirealms.customcrops.api.BukkitCustomCropsPlugin;
import net.momirealms.customcrops.common.helper.AdventureHelper;
import net.momirealms.customcrops.common.helper.VersionHelper;
import net.momirealms.customcrops.common.plugin.feature.Reloadable;
import net.momirealms.customcrops.common.plugin.scheduler.SchedulerTask;
import net.momirealms.customcrops.common.util.Pair;
import net.momirealms.sparrow.heart.SparrowHeart;
import net.momirealms.sparrow.heart.feature.entity.FakeEntity;
import net.momirealms.sparrow.heart.feature.entity.armorstand.FakeArmorStand;
import net.momirealms.sparrow.heart.feature.entity.display.FakeTextDisplay;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.HandlerList;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerQuitEvent;
import java.util.ArrayList;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.TimeUnit;
public class HologramManager implements Listener, Reloadable {
private final ConcurrentHashMap<UUID, HologramCache> hologramMap = new ConcurrentHashMap<>();
private final BukkitCustomCropsPlugin plugin;
private SchedulerTask cacheCheckTask;
private static HologramManager manager;
public static HologramManager getInstance() {
return manager;
}
public HologramManager(BukkitCustomCropsPlugin plugin) {
this.plugin = plugin;
manager = this;
}
@Override
public void load() {
Bukkit.getPluginManager().registerEvents(this, plugin.getBoostrap());
this.cacheCheckTask = plugin.getScheduler().asyncRepeating(() -> {
ArrayList<UUID> removed = new ArrayList<>();
long current = System.currentTimeMillis();
for (Map.Entry<UUID, HologramCache> entry : hologramMap.entrySet()) {
Player player = Bukkit.getPlayer(entry.getKey());
if (player == null || !player.isOnline()) {
removed.add(entry.getKey());
} else {
entry.getValue().removeOutDated(current, player);
}
}
for (UUID uuid : removed) {
hologramMap.remove(uuid);
}
}, 100, 100, TimeUnit.MILLISECONDS);
}
@Override
public void unload() {
HandlerList.unregisterAll(this);
for (Map.Entry<UUID, HologramCache> entry : hologramMap.entrySet()) {
Player player = Bukkit.getPlayer(entry.getKey());
if (player != null && player.isOnline()) {
entry.getValue().removeAll(player);
}
}
if (cacheCheckTask != null) cacheCheckTask.cancel();
this.hologramMap.clear();
}
@EventHandler
public void onQuit(PlayerQuitEvent event) {
this.hologramMap.remove(event.getPlayer().getUniqueId());
}
public void showHologram(Player player, Location location, Component component, int millis) {
HologramCache hologramCache = hologramMap.get(player.getUniqueId());
if (hologramCache != null) {
hologramCache.showHologram(player, location, component, millis);
} else {
hologramCache = new HologramCache();
hologramCache.showHologram(player, location, component, millis);
hologramMap.put(player.getUniqueId(), hologramCache);
}
}
public static class HologramCache {
private final ConcurrentHashMap<Location, Pair<FakeEntity, Long>> cache = new ConcurrentHashMap<>();
public void removeOutDated(long current, Player player) {
ArrayList<Location> removed = new ArrayList<>();
for (Map.Entry<Location, Pair<FakeEntity, Long>> entry : cache.entrySet()) {
if (entry.getValue().right() < current) {
entry.getValue().left().destroy(player);
removed.add(entry.getKey());
}
}
for (Location location : removed) {
cache.remove(location);
}
}
public void showHologram(Player player, Location location, Component component, int millis) {
Pair<FakeEntity, Long> pair = cache.get(location);
if (pair != null) {
pair.left().destroy(player);
pair.right(System.currentTimeMillis() + millis);
if (VersionHelper.isVersionNewerThan1_19_4()) {
FakeTextDisplay fakeEntity = SparrowHeart.getInstance().createFakeTextDisplay(location.clone().add(0,1.25,0));
fakeEntity.name(AdventureHelper.componentToJson(component));
fakeEntity.rgba(0, 0, 0, 0);
fakeEntity.spawn(player);
pair.left(fakeEntity);
} else {
FakeArmorStand fakeEntity = SparrowHeart.getInstance().createFakeArmorStand(location);
fakeEntity.name(AdventureHelper.componentToJson(component));
fakeEntity.small(true);
fakeEntity.invisible(true);
fakeEntity.spawn(player);
pair.left(fakeEntity);
}
} else {
long removeTime = System.currentTimeMillis() + millis;
if (VersionHelper.isVersionNewerThan1_19_4()) {
FakeTextDisplay fakeEntity = SparrowHeart.getInstance().createFakeTextDisplay(location.clone().add(0,1.25,0));
fakeEntity.name(AdventureHelper.componentToJson(component));
fakeEntity.rgba(0, 0, 0, 0);
fakeEntity.spawn(player);
this.cache.put(location, Pair.of(fakeEntity, removeTime));
} else {
FakeArmorStand fakeEntity = SparrowHeart.getInstance().createFakeArmorStand(location);
fakeEntity.name(AdventureHelper.componentToJson(component));
fakeEntity.small(true);
fakeEntity.invisible(true);
fakeEntity.spawn(player);
this.cache.put(location, Pair.of(fakeEntity, removeTime));
}
}
}
public void removeAll(Player player) {
for (Map.Entry<Location, Pair<FakeEntity, Long>> entry : this.cache.entrySet()) {
entry.getValue().left().destroy(player);
}
cache.clear();
}
}
}

View File

@@ -20,6 +20,7 @@ package net.momirealms.customcrops.bukkit.requirement;
import dev.dejvokep.boostedyaml.block.implementation.Section;
import net.momirealms.customcrops.api.BukkitCustomCropsPlugin;
import net.momirealms.customcrops.api.action.ActionManager;
import net.momirealms.customcrops.api.context.ContextKeys;
import net.momirealms.customcrops.api.integration.LevelerProvider;
import net.momirealms.customcrops.api.misc.value.MathValue;
import net.momirealms.customcrops.api.requirement.AbstractRequirementManager;
@@ -27,6 +28,7 @@ import net.momirealms.customcrops.api.requirement.Requirement;
import net.momirealms.customcrops.bukkit.integration.VaultHook;
import net.momirealms.customcrops.common.util.ListUtils;
import org.bukkit.entity.Player;
import org.bukkit.inventory.EquipmentSlot;
import org.bukkit.inventory.ItemStack;
import org.bukkit.potion.PotionEffect;
import org.bukkit.potion.PotionEffectType;
@@ -63,13 +65,18 @@ public class PlayerRequirementManager extends AbstractRequirementManager<Player>
registerRequirement((args, actions, runActions) -> {
if (args instanceof Section section) {
boolean mainOrOff = section.getString("hand","main").equalsIgnoreCase("main");
int amount = section.getInt("amount", 1);
int amount = section.getInt("amount", 0);
List<String> items = ListUtils.toList(section.get("item"));
return context -> {
if (context.holder() == null) return true;
ItemStack itemStack = mainOrOff ?
context.holder().getInventory().getItemInMainHand()
: context.holder().getInventory().getItemInOffHand();
Player player = context.holder();
if (player == null) return true;
EquipmentSlot slot = context.arg(ContextKeys.SLOT);
ItemStack itemStack;
if (slot == null) {
itemStack = mainOrOff ? player.getInventory().getItemInMainHand() : player.getInventory().getItemInOffHand();
} else {
itemStack = player.getInventory().getItem(slot);
}
String id = plugin.getItemManager().id(itemStack);
if (items.contains(id) && itemStack.getAmount() >= amount) return true;
if (runActions) ActionManager.trigger(context, actions);