9
0
mirror of https://github.com/Xiao-MoMi/Custom-Crops.git synced 2025-12-22 08:29:35 +00:00
This commit is contained in:
Xiao-MoMi
2022-10-06 02:44:52 +08:00
parent 7bb18a4925
commit 680c1119a8
17 changed files with 423 additions and 81 deletions

View File

@@ -292,6 +292,12 @@ public abstract class HandlerP extends Function {
return StringUtils.chop(id) + nextStage;
}
public void placeScareCrow(Location location) {
CustomWorld customWorld = cropManager.getCustomWorld(location.getWorld());
if (customWorld == null) return;
}
public boolean fillWaterCan(String id, NBTItem nbtItem, ItemStack itemStack, Player player) {
WaterCan config = WaterCanConfig.CANS.get(id);
if (config != null) {

View File

@@ -19,6 +19,7 @@ package net.momirealms.customcrops.integrations.customplugin.itemsadder.listener
import dev.lone.itemsadder.api.Events.FurnitureBreakEvent;
import dev.lone.itemsadder.api.Events.FurnitureInteractEvent;
import dev.lone.itemsadder.api.Events.FurniturePlaceEvent;
import net.momirealms.customcrops.integrations.customplugin.itemsadder.ItemsAdderHandler;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
@@ -40,4 +41,9 @@ public class ItemsAdderFurnitureListener implements Listener {
public void onBreakFurniture(FurnitureBreakEvent event) {
handler.onBreakFurniture(event);
}
@EventHandler
public void onPlaceFurniture(FurniturePlaceEvent event) {
//TODO scarecrow
}
}

View File

@@ -110,7 +110,11 @@ public class OraxenFrameHandler extends OraxenHandler {
super.onBreakSprinkler(event.getBlock().getLocation());
return;
}
if (MainConfig.enableCrow && id.equals(BasicItemConfig.scarecrow)) {
super.removeScarecrow(event.getBlock().getLocation());
return;
}
//TODO check if event.getBlock()
if (id.contains("_stage_")) {
if (id.equals(BasicItemConfig.deadCrop)) return;
if (hasNextStage(id)) {

View File

@@ -21,17 +21,16 @@ import de.tr7zw.changeme.nbtapi.NBTCompound;
import de.tr7zw.changeme.nbtapi.NBTItem;
import io.th0rgal.oraxen.events.*;
import io.th0rgal.oraxen.items.OraxenItems;
import io.th0rgal.oraxen.mechanics.provided.gameplay.furniture.FurnitureMechanic;
import net.momirealms.customcrops.CustomCrops;
import net.momirealms.customcrops.api.crop.Crop;
import net.momirealms.customcrops.api.event.WaterEvent;
import net.momirealms.customcrops.config.CropConfig;
import net.momirealms.customcrops.config.MainConfig;
import net.momirealms.customcrops.config.SoundConfig;
import net.momirealms.customcrops.config.WaterCanConfig;
import net.momirealms.customcrops.config.*;
import net.momirealms.customcrops.integrations.customplugin.HandlerP;
import net.momirealms.customcrops.integrations.customplugin.oraxen.listeners.OraxenBlockListener;
import net.momirealms.customcrops.integrations.customplugin.oraxen.listeners.OraxenFurnitureListener;
import net.momirealms.customcrops.managers.CropManager;
import net.momirealms.customcrops.managers.CustomWorld;
import net.momirealms.customcrops.objects.WaterCan;
import net.momirealms.customcrops.utils.AdventureUtil;
import org.apache.commons.lang.StringUtils;
@@ -73,6 +72,24 @@ public abstract class OraxenHandler extends HandlerP {
HandlerList.unregisterAll(this.oraxenFurnitureListener);
}
//scarecrow place
public void placeScarecrow(OraxenFurniturePlaceEvent event) {
if (!MainConfig.enableCrow) return;
FurnitureMechanic mechanic = event.getFurnitureMechanic();
String id = mechanic.getItemID();
if (!id.equals(BasicItemConfig.scarecrow)) return;
Location location = event.getItemFrame().getLocation();
CustomWorld customWorld = cropManager.getCustomWorld(location.getWorld());
if (customWorld == null) return;
customWorld.addScarecrow(location);
}
public void removeScarecrow(Location location) {
CustomWorld customWorld = cropManager.getCustomWorld(location.getWorld());
if (customWorld == null) return;
customWorld.removeScarecrow(location);
}
public boolean tryMisc(Player player, ItemStack itemInHand, Location potLoc) {
if (itemInHand == null || itemInHand.getType() == Material.AIR) return true;
String id = OraxenItems.getIdByItem(itemInHand);

View File

@@ -138,13 +138,17 @@ public class OraxenWireHandler extends OraxenHandler{
if (event.isCancelled()) return;
//TODO Check if triggered in res
//System.out.println(1);
FurnitureMechanic mechanic = event.getFurnitureMechanic();
if (mechanic == null) return;
Sprinkler sprinkler = SprinklerConfig.SPRINKLERS_3D.get(mechanic.getItemID());
String id = mechanic.getItemID();
Sprinkler sprinkler = SprinklerConfig.SPRINKLERS_3D.get(id);
if (sprinkler != null) {
super.onBreakSprinkler(event.getBlock().getLocation());
return;
}
if (MainConfig.enableCrow && id.equals(BasicItemConfig.scarecrow)) {
super.removeScarecrow(event.getBlock().getLocation());
}
}
@@ -239,8 +243,6 @@ public class OraxenWireHandler extends OraxenHandler{
@Override
public void onInteractStringBlock(OraxenStringBlockInteractEvent event) {
if (event.isCancelled()) return;
final Player player = event.getPlayer();
long time = System.currentTimeMillis();
@@ -253,7 +255,6 @@ public class OraxenWireHandler extends OraxenHandler{
if (id.contains("_stage_")) {
Location seedLoc = block.getLocation();
ItemStack itemInHand = event.getItemInHand();
//ripe crops
if (!id.equals(BasicItemConfig.deadCrop)) {

View File

@@ -19,6 +19,7 @@ package net.momirealms.customcrops.integrations.customplugin.oraxen.listeners;
import io.th0rgal.oraxen.events.OraxenFurnitureBreakEvent;
import io.th0rgal.oraxen.events.OraxenFurnitureInteractEvent;
import io.th0rgal.oraxen.events.OraxenFurniturePlaceEvent;
import net.momirealms.customcrops.integrations.customplugin.oraxen.OraxenHandler;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
@@ -32,12 +33,17 @@ public class OraxenFurnitureListener implements Listener {
}
@EventHandler
public void onInteract(OraxenFurnitureInteractEvent event) {
public void onInteractFurniture(OraxenFurnitureInteractEvent event) {
handler.onInteractFurniture(event);
}
@EventHandler
public void onBreak(OraxenFurnitureBreakEvent event) {
public void onBreakFurniture(OraxenFurnitureBreakEvent event) {
handler.onBreakFurniture(event);
}
@EventHandler
public void onPlaceFurniture(OraxenFurniturePlaceEvent event) {
handler.placeScarecrow(event);
}
}

View File

@@ -89,19 +89,27 @@ public class CropManager extends Function {
//Custom Plugin
if (MainConfig.customPlugin.equals("itemsadder")) {
customInterface = new ItemsAdderHook();
if (MainConfig.cropMode) this.handler = new ItemsAdderWireHandler(this);
else this.handler = new ItemsAdderFrameHandler(this);
if (MainConfig.cropMode) {
this.handler = new ItemsAdderWireHandler(this);
this.cropMode = new ItemsAdderWireCropImpl(this);
}
else {
this.handler = new ItemsAdderFrameHandler(this);
this.cropMode = new ItemsAdderFrameCropImpl(this);
}
}
else if (MainConfig.customPlugin.equals("oraxen")){
customInterface = new OraxenHook();
if (MainConfig.cropMode) this.handler = new OraxenWireHandler(this);
else this.handler = new OraxenFrameHandler(this);
if (MainConfig.cropMode) {
this.handler = new OraxenWireHandler(this);
this.cropMode = new OraxenWireCropImpl(this);
}
else {
this.handler = new OraxenFrameHandler(this);
this.cropMode = new OraxenFrameCropImpl(this);
}
}
//Crop mode
if (MainConfig.cropMode) this.cropMode = new WireCropImpl(this);
else this.cropMode = new FrameCropImpl(this);
//new Time Check task
this.timerTask = new TimerTask(this);
this.timerTask.runTaskTimerAsynchronously(CustomCrops.plugin, 1,100);
@@ -125,14 +133,12 @@ public class CropManager extends Function {
HandlerList.unregisterAll(this.itemSpawnListener);
HandlerList.unregisterAll(this.worldListener);
if (this.handler != null) handler.unload();
this.timerTask.cancel();
if (this.timerTask != null) this.timerTask.cancel();
for (CustomWorld customWorld : customWorlds.values()) {
customWorld.unload(true);
}
customWorlds.clear();
if (this.seasonInterface != null) {
seasonInterface.unload();
}
if (this.seasonInterface != null) seasonInterface.unload();
}
public void onItemSpawn(Item item) {
@@ -148,7 +154,7 @@ public class CropManager extends Function {
CustomWorld customWorld = new CustomWorld(world, this);
customWorlds.put(world, customWorld);
if (MainConfig.autoGrow && MainConfig.enableCompensation) {
if (world.getTime() < 24000 - MainConfig.timeToWork - MainConfig.timeToDry - 1200 && world.getTime() > 1500) {
if (world.getTime() < 24000 - MainConfig.timeToWork - MainConfig.timeToDry - 1200 && world.getTime() > 1200) {
Bukkit.getScheduler().runTaskLaterAsynchronously(CustomCrops.plugin, () -> grow(world, MainConfig.timeToGrow, MainConfig.timeToWork, MainConfig.timeToDry, true), 100);
}
}

View File

@@ -17,11 +17,15 @@
package net.momirealms.customcrops.managers;
import org.bukkit.Chunk;
import org.bukkit.Location;
public interface CropModeInterface {
boolean growJudge(Location location);
void loadChunk(Location location);
}
default void loadChunk(Location location) {
Chunk chunk = location.getChunk();
chunk.load();
}
}

View File

@@ -32,6 +32,7 @@ import net.momirealms.customcrops.utils.MiscUtils;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.StringUtils;
import org.bukkit.Bukkit;
import org.bukkit.Chunk;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.configuration.file.YamlConfiguration;
@@ -51,6 +52,7 @@ public class CustomWorld {
private final ConcurrentHashMap<SimpleLocation, String> cropCache;
private final ConcurrentHashMap<SimpleLocation, Sprinkler> sprinklerCache;
private final ConcurrentHashMap<SimpleLocation, Fertilizer> fertilizerCache;
private final ConcurrentHashMap<String, HashSet<SimpleLocation>> scarecrowCache;
private final Set<SimpleLocation> watered;
private HashSet<SimpleLocation> tempWatered;
private final HashSet<SimpleLocation> playerWatered;
@@ -64,17 +66,14 @@ public class CustomWorld {
this.fertilizerCache = new ConcurrentHashMap<>(2048);
this.sprinklerCache = new ConcurrentHashMap<>(1024);
this.tasksCache = new HashSet<>(4096);
this.scarecrowCache = new ConcurrentHashMap<>(256);
this.cropManager = cropManager;
this.bukkitScheduler = Bukkit.getScheduler();
this.watered = Collections.synchronizedSet(new HashSet<>());
this.playerWatered = new HashSet<>();
this.tempWatered = new HashSet<>();
Bukkit.getScheduler().runTaskAsynchronously(CustomCrops.plugin, () -> {
loadSeason();
loadCropCache();
loadSprinklerCache();
loadFertilizerCache();
loadPot();
loadData();
Bukkit.getScheduler().runTask(CustomCrops.plugin, () -> {
CustomWorldEvent customWorldEvent = new CustomWorldEvent(world, WorldState.LOAD);
Bukkit.getPluginManager().callEvent(customWorldEvent);
@@ -84,21 +83,11 @@ public class CustomWorld {
public void unload(boolean disable) {
if (disable) {
unloadSeason();
unloadCrop();
unloadSprinkler();
unloadFertilizer();
unloadPot();
backUp(world.getName());
unloadData();
}
else {
Bukkit.getScheduler().runTaskAsynchronously(CustomCrops.plugin, () -> {
unloadSeason();
unloadCrop();
unloadSprinkler();
unloadFertilizer();
unloadPot();
backUp(world.getName());
unloadData();
for (BukkitTask task : tasksCache) {
task.cancel();
}
@@ -111,7 +100,27 @@ public class CustomWorld {
}
}
private void loadData() {
loadSeason();
loadCropCache();
loadSprinklerCache();
loadFertilizerCache();
loadPot();
loadScarecrow();
}
private void unloadData() {
unloadSeason();
unloadCrop();
unloadSprinkler();
unloadFertilizer();
unloadPot();
unloadScarecrow();
backUp(world.getName());
}
private void backUp(String worldName) {
if (!MainConfig.autoBackUp) return;
Date date = new Date();
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss");
try {
@@ -128,7 +137,53 @@ public class CustomWorld {
}
}
public void loadSeason() {
private void loadScarecrow() {
if (!MainConfig.enableCrow) return;
try {
JsonParser jsonParser = new JsonParser();
JsonElement json= jsonParser.parse(new FileReader(new File(CustomCrops.plugin.getDataFolder().getParentFile().getParentFile(), world.getName() + File.separator + "customcrops_data" + File.separator + "scarecrow.json")));
if (json.isJsonObject()) {
JsonObject jsonObject = json.getAsJsonObject();
for (Map.Entry<String, JsonElement> en : jsonObject.entrySet()) {
JsonArray jsonArray = en.getValue().getAsJsonArray();
int size = jsonArray.size();
HashSet<SimpleLocation> simpleLocations = new HashSet<>();
for (int i = 0; i < size; i++) {
simpleLocations.add(MiscUtils.getSimpleLocation(jsonArray.get(i).getAsString(), world.getName()));
}
scarecrowCache.put(en.getKey(), simpleLocations);
}
}
SeasonUtils.setSeason(world, CCSeason.UNKNOWN);
}
catch (FileNotFoundException e) {
//bypass
}
}
private void unloadScarecrow() {
if (!MainConfig.enableCrow) return;
JsonObject jsonObject = new JsonObject();
for (Map.Entry<String, HashSet<SimpleLocation>> entry : scarecrowCache.entrySet()) {
HashSet<SimpleLocation> locations = entry.getValue();
JsonArray jsonArray = new JsonArray();
for (SimpleLocation simpleLocation : locations) {
String loc = simpleLocation.getX() + "," + simpleLocation.getY() + "," + simpleLocation.getZ();
jsonArray.add(new JsonPrimitive(loc));
}
jsonObject.add(entry.getKey(), jsonArray);
}
try (FileWriter fileWriter = new FileWriter(new File(CustomCrops.plugin.getDataFolder().getParentFile().getParentFile(), world.getName() + File.separator + "customcrops_data" + File.separator + "scarecrow.json"))){
fileWriter.write(jsonObject.toString().replace("\\\\", "\\"));
} catch (IOException e) {
e.printStackTrace();
}
SeasonUtils.unloadSeason(world);
}
private void loadSeason() {
if (!SeasonConfig.enable) return;
try {
JsonParser jsonParser = new JsonParser();
JsonElement json= jsonParser.parse(new FileReader(new File(CustomCrops.plugin.getDataFolder().getParentFile().getParentFile(), world.getName() + File.separator + "customcrops_data" + File.separator + "season.json")));
@@ -149,7 +204,8 @@ public class CustomWorld {
}
}
public void unloadSeason() {
private void unloadSeason() {
if (!SeasonConfig.enable) return;
JsonObject jsonObject = new JsonObject();
JsonPrimitive jsonPrimitive = new JsonPrimitive(SeasonUtils.getSeason(world).name());
jsonObject.add("season", jsonPrimitive);
@@ -161,7 +217,7 @@ public class CustomWorld {
SeasonUtils.unloadSeason(world);
}
public void loadPot() {
private void loadPot() {
try {
JsonParser jsonParser = new JsonParser();
JsonElement json= jsonParser.parse(new FileReader(new File(CustomCrops.plugin.getDataFolder().getParentFile().getParentFile(), world.getName() + File.separator + "customcrops_data" + File.separator + "pot.json")));
@@ -183,7 +239,7 @@ public class CustomWorld {
}
}
public void unloadPot() {
private void unloadPot() {
JsonObject jsonObject = new JsonObject();
JsonArray jsonArray = new JsonArray();
watered.addAll(playerWatered);
@@ -470,4 +526,30 @@ public class CustomWorld {
public void setPlayerWatered(Location location) {
playerWatered.add(MiscUtils.getSimpleLocation(location));
}
public boolean hasScarecrow(Location location) {
Chunk chunk = location.getChunk();
return scarecrowCache.containsKey(chunk.getX() + "," + chunk.getZ());
}
public void addScarecrow(Location location) {
Chunk chunk = location.getChunk();
HashSet<SimpleLocation> old = scarecrowCache.get(chunk.getX() + "," + chunk.getZ());
if (old == null) {
HashSet<SimpleLocation> young = new HashSet<>(4);
young.add(new SimpleLocation(world.getName(), location.getBlockX(), location.getBlockY(), location.getBlockZ()));
scarecrowCache.put(chunk.getX() + "," + chunk.getZ(), young);
}
else {
old.add(new SimpleLocation(world.getName(), location.getBlockX(), location.getBlockY(), location.getBlockZ()));
}
}
public void removeScarecrow(Location location) {
Chunk chunk = location.getChunk();
HashSet<SimpleLocation> old = scarecrowCache.get(chunk.getX() + "," + chunk.getZ());
if (old == null) return;
old.remove(new SimpleLocation(world.getName(), location.getBlockX(), location.getBlockY(), location.getBlockZ()));
if (old.size() == 0) scarecrowCache.remove(chunk.getX() + "," + chunk.getZ());
}
}

View File

@@ -0,0 +1,117 @@
/*
* Copyright (C) <2022> <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.managers;
import net.momirealms.customcrops.CustomCrops;
import net.momirealms.customcrops.api.crop.Crop;
import net.momirealms.customcrops.config.BasicItemConfig;
import net.momirealms.customcrops.config.CropConfig;
import net.momirealms.customcrops.config.MainConfig;
import net.momirealms.customcrops.integrations.customplugin.CustomInterface;
import net.momirealms.customcrops.objects.GiganticCrop;
import net.momirealms.customcrops.objects.fertilizer.Fertilizer;
import net.momirealms.customcrops.objects.fertilizer.Gigantic;
import net.momirealms.customcrops.objects.fertilizer.SpeedGrow;
import net.momirealms.customcrops.utils.FurnitureUtil;
import org.apache.commons.lang.StringUtils;
import org.bukkit.Bukkit;
import org.bukkit.Chunk;
import org.bukkit.Location;
import org.bukkit.entity.ItemFrame;
public class ItemsAdderFrameCropImpl implements CropModeInterface {
private final CropManager cropManager;
private final CustomInterface customInterface;
public ItemsAdderFrameCropImpl(CropManager cropManager) {
this.cropManager = cropManager;
this.customInterface = cropManager.getCustomInterface();
}
@Override
public boolean growJudge(Location location) {
Chunk chunk = location.getChunk();
if (chunk.isEntitiesLoaded()) {
Location cropLoc = location.clone().add(0.5,0.5,0.5);
ItemFrame itemFrame = FurnitureUtil.getItemFrame(cropLoc);
if (itemFrame == null) return true;
String id = customInterface.getItemID(itemFrame.getItem());
if (id == null) return true;
if (id.equals(BasicItemConfig.deadCrop)) return true;
String[] cropNameList = StringUtils.split(id,"_");
String cropKey = StringUtils.split(cropNameList[0], ":")[1];
Crop crop = CropConfig.CROPS.get(cropKey);
if (crop == null) return true;
if (cropManager.isWrongSeason(location, crop.getSeasons())) {
itemFrame.setItem(customInterface.getItemStack(BasicItemConfig.deadCrop), false);
return true;
}
Location potLoc = location.clone().subtract(0,1,0);
String potID = customInterface.getBlockID(potLoc);
if (potID == null) return true;
Fertilizer fertilizer = cropManager.getFertilizer(potLoc);
boolean certainGrow = potID.equals(BasicItemConfig.wetPot);
int nextStage = Integer.parseInt(cropNameList[2]) + 1;
String temp = StringUtils.chop(id);
if (customInterface.doesExist(temp + nextStage)) {
if (fertilizer instanceof SpeedGrow speedGrow && Math.random() < speedGrow.getChance()) {
if (customInterface.doesExist(temp + (nextStage+1))) {
addStage(itemFrame, temp + (nextStage+1));
}
}
else if (certainGrow || Math.random() < MainConfig.dryGrowChance) {
addStage(itemFrame, temp + nextStage);
}
}
else {
GiganticCrop giganticCrop = crop.getGiganticCrop();
if (giganticCrop != null) {
double chance = giganticCrop.getChance();
if (fertilizer instanceof Gigantic gigantic) {
chance += gigantic.getChance();
}
if (Math.random() < chance) {
Bukkit.getScheduler().runTask(CustomCrops.plugin, () -> {
customInterface.removeFurniture(itemFrame);
if (giganticCrop.isBlock()) {
customInterface.placeWire(location, giganticCrop.getBlockID());
}
else {
customInterface.placeFurniture(location, giganticCrop.getBlockID());
}
});
}
}
return true;
}
}
return false;
}
private void addStage(ItemFrame itemFrame, String stage) {
itemFrame.setItem(customInterface.getItemStack(stage), false);
}
}

View File

@@ -29,33 +29,25 @@ import net.momirealms.customcrops.objects.fertilizer.Gigantic;
import net.momirealms.customcrops.objects.fertilizer.SpeedGrow;
import org.apache.commons.lang.StringUtils;
import org.bukkit.Bukkit;
import org.bukkit.Chunk;
import org.bukkit.Location;
public class WireCropImpl implements CropModeInterface{
public class ItemsAdderWireCropImpl implements CropModeInterface{
private final CropManager cropManager;
private final CustomInterface customInterface;
public WireCropImpl(CropManager cropManager) {
public ItemsAdderWireCropImpl(CropManager cropManager) {
this.cropManager = cropManager;
this.customInterface = cropManager.getCustomInterface();
}
@Override
public void loadChunk(Location location) {
Chunk chunk = location.getChunk();
chunk.load();
}
@Override
public boolean growJudge(Location location) {
String blockID = customInterface.getBlockID(location);
if (blockID == null) return true;
if (!blockID.contains("_stage_")) return true;
String[] cropNameList = StringUtils.split(blockID,"_");
String cropKey = cropNameList[0];
if (cropKey.contains(":")) cropKey = StringUtils.split(cropKey, ":")[1];
String cropKey = StringUtils.split(cropNameList[0], ":")[1];
Crop crop = CropConfig.CROPS.get(cropKey);
if (crop == null) return true;
if (cropManager.isWrongSeason(location, crop.getSeasons())) {
@@ -110,7 +102,7 @@ public class WireCropImpl implements CropModeInterface{
private void addStage(Location seedLoc, String stage) {
Bukkit.getScheduler().runTask(CustomCrops.plugin, () -> {
if (!MainConfig.OraxenHook) customInterface.removeBlock(seedLoc);
customInterface.removeBlock(seedLoc);
customInterface.placeWire(seedLoc, stage);
});
}

View File

@@ -36,22 +36,16 @@ import org.bukkit.Location;
import org.bukkit.entity.ItemFrame;
import org.bukkit.persistence.PersistentDataType;
public class FrameCropImpl implements CropModeInterface {
public class OraxenFrameCropImpl implements CropModeInterface {
private final CropManager cropManager;
private final CustomInterface customInterface;
public FrameCropImpl(CropManager cropManager) {
public OraxenFrameCropImpl(CropManager cropManager) {
this.cropManager = cropManager;
this.customInterface = cropManager.getCustomInterface();
}
@Override
public void loadChunk(Location location) {
Chunk chunk = location.getChunk();
chunk.load();
}
@Override
public boolean growJudge(Location location) {
@@ -59,10 +53,7 @@ public class FrameCropImpl implements CropModeInterface {
if (chunk.isEntitiesLoaded()) {
Location cropLoc;
if (MainConfig.OraxenHook) cropLoc = location.clone().add(0.5,0.03125,0.5);
else cropLoc = location.clone().add(0.5,0.5,0.5);
Location cropLoc = location.clone().add(0.5,0.03125,0.5);
ItemFrame itemFrame = FurnitureUtil.getItemFrame(cropLoc);
if (itemFrame == null) return true;
String id = customInterface.getItemID(itemFrame.getItem());
@@ -71,12 +62,11 @@ public class FrameCropImpl implements CropModeInterface {
String[] cropNameList = StringUtils.split(id,"_");
String cropKey = cropNameList[0];
if (cropKey.contains(":")) cropKey = StringUtils.split(cropKey, ":")[1];
Crop crop = CropConfig.CROPS.get(cropKey);
if (crop == null) return true;
if (cropManager.isWrongSeason(location, crop.getSeasons())) {
itemFrame.setItem(customInterface.getItemStack(BasicItemConfig.deadCrop));
if (MainConfig.OraxenHook) itemFrame.getPersistentDataContainer().set(OraxenHook.FURNITURE, PersistentDataType.STRING, BasicItemConfig.deadCrop);
itemFrame.setItem(customInterface.getItemStack(BasicItemConfig.deadCrop), false);
itemFrame.getPersistentDataContainer().set(OraxenHook.FURNITURE, PersistentDataType.STRING, BasicItemConfig.deadCrop);
return true;
}
@@ -125,7 +115,7 @@ public class FrameCropImpl implements CropModeInterface {
}
private void addStage(ItemFrame itemFrame, String stage) {
itemFrame.setItem(customInterface.getItemStack(stage));
if (MainConfig.OraxenHook) itemFrame.getPersistentDataContainer().set(OraxenHook.FURNITURE, PersistentDataType.STRING, stage);
itemFrame.setItem(customInterface.getItemStack(stage), false);
itemFrame.getPersistentDataContainer().set(OraxenHook.FURNITURE, PersistentDataType.STRING, stage);
}
}

View File

@@ -0,0 +1,105 @@
/*
* Copyright (C) <2022> <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.managers;
import net.momirealms.customcrops.CustomCrops;
import net.momirealms.customcrops.api.crop.Crop;
import net.momirealms.customcrops.config.BasicItemConfig;
import net.momirealms.customcrops.config.CropConfig;
import net.momirealms.customcrops.config.MainConfig;
import net.momirealms.customcrops.integrations.customplugin.CustomInterface;
import net.momirealms.customcrops.objects.GiganticCrop;
import net.momirealms.customcrops.objects.fertilizer.Fertilizer;
import net.momirealms.customcrops.objects.fertilizer.Gigantic;
import net.momirealms.customcrops.objects.fertilizer.SpeedGrow;
import org.apache.commons.lang.StringUtils;
import org.bukkit.Bukkit;
import org.bukkit.Location;
public class OraxenWireCropImpl implements CropModeInterface{
private final CropManager cropManager;
private final CustomInterface customInterface;
public OraxenWireCropImpl(CropManager cropManager) {
this.cropManager = cropManager;
this.customInterface = cropManager.getCustomInterface();
}
@Override
public boolean growJudge(Location location) {
String blockID = customInterface.getBlockID(location);
if (blockID == null) return true;
if (!blockID.contains("_stage_")) return true;
String[] cropNameList = StringUtils.split(blockID,"_");
String cropKey = cropNameList[0];
Crop crop = CropConfig.CROPS.get(cropKey);
if (crop == null) return true;
if (cropManager.isWrongSeason(location, crop.getSeasons())) {
Bukkit.getScheduler().runTask(CustomCrops.plugin, () -> {
customInterface.removeBlock(location);
customInterface.placeWire(location, BasicItemConfig.deadCrop);
});
return true;
}
Location potLoc = location.clone().subtract(0,1,0);
String potID = customInterface.getBlockID(potLoc);
if (potID == null) return true;
Fertilizer fertilizer = cropManager.getFertilizer(potLoc);
boolean certainGrow = potID.equals(BasicItemConfig.wetPot);
int nextStage = Integer.parseInt(cropNameList[2]) + 1;
String temp = StringUtils.chop(blockID);
if (customInterface.doesExist(temp + nextStage)) {
if (fertilizer instanceof SpeedGrow speedGrow && Math.random() < speedGrow.getChance()) {
if (customInterface.doesExist(temp + (nextStage+1))) {
addStage(location, temp + (nextStage+1));
}
}
else if (certainGrow || Math.random() < MainConfig.dryGrowChance) {
addStage(location, temp + nextStage);
}
}
else {
GiganticCrop giganticCrop = crop.getGiganticCrop();
if (giganticCrop != null) {
double chance = giganticCrop.getChance();
if (fertilizer instanceof Gigantic gigantic) {
chance += gigantic.getChance();
}
if (Math.random() < chance) {
Bukkit.getScheduler().runTask(CustomCrops.plugin, () -> {
customInterface.removeBlock(location);
if (giganticCrop.isBlock()) {
customInterface.placeWire(location, giganticCrop.getBlockID());
}
else {
customInterface.placeFurniture(location, giganticCrop.getBlockID());
}
});
}
}
return true;
}
return false;
}
private void addStage(Location seedLoc, String stage) {
Bukkit.getScheduler().runTask(CustomCrops.plugin, () -> customInterface.placeWire(seedLoc, stage));
}
}

View File

@@ -18,6 +18,7 @@
package net.momirealms.customcrops.utils;
import net.momirealms.customcrops.objects.SimpleLocation;
import org.apache.commons.lang.StringUtils;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.World;
@@ -35,4 +36,9 @@ public class MiscUtils {
if (world == null) return null;
return new Location(world, location.getX(), location.getY(), location.getZ());
}
public static SimpleLocation getSimpleLocation(String location, String world) {
String[] loc = StringUtils.split(location, ",");
return new SimpleLocation(world, Integer.parseInt(loc[0]), Integer.parseInt(loc[1]), Integer.parseInt(loc[2]));
}
}