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:
XiaoMoMi
2024-03-12 01:24:39 +08:00
parent 1c64f84ec9
commit 9706fef822
15 changed files with 212 additions and 81 deletions

View File

@@ -20,6 +20,7 @@ package net.momirealms.customcrops.api.manager;
import net.momirealms.customcrops.api.common.Reloadable;
import net.momirealms.customcrops.api.integration.ItemLibrary;
import net.momirealms.customcrops.api.mechanic.item.*;
import net.momirealms.customcrops.api.mechanic.misc.CRotation;
import org.bukkit.Location;
import org.bukkit.block.Block;
import org.bukkit.entity.Entity;
@@ -42,9 +43,9 @@ public interface ItemManager extends Reloadable {
void placeItem(Location location, ItemCarrier carrier, String id);
void placeItem(Location location, ItemCarrier carrier, String id, boolean rotate);
void placeItem(Location location, ItemCarrier carrier, String id, CRotation rotate);
void removeAnythingAt(Location location);
CRotation removeAnythingAt(Location location);
@Nullable
WateringCan getWateringCanByID(@NotNull String id);

View File

@@ -0,0 +1,21 @@
package net.momirealms.customcrops.api.mechanic.misc;
public enum CRotation {
NONE(0f),
RANDOM(0f),
EAST(-90f),
SOUTH(0f),
WEST(90f),
NORTH(180f);
private final float yaw;
CRotation(float yaw) {
this.yaw = yaw;
}
public float getYaw() {
return yaw;
}
}

View File

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

View File

@@ -35,6 +35,7 @@ import net.momirealms.customcrops.api.mechanic.item.*;
import net.momirealms.customcrops.api.mechanic.item.fertilizer.QualityCrop;
import net.momirealms.customcrops.api.mechanic.item.fertilizer.Variation;
import net.momirealms.customcrops.api.mechanic.item.fertilizer.YieldIncrease;
import net.momirealms.customcrops.api.mechanic.misc.CRotation;
import net.momirealms.customcrops.api.mechanic.misc.Value;
import net.momirealms.customcrops.api.mechanic.requirement.Requirement;
import net.momirealms.customcrops.api.mechanic.world.ChunkCoordinate;
@@ -281,6 +282,7 @@ public class ActionManagerImpl implements ActionManager {
ArrayList<String> msg = ConfigUtils.stringListArgs(args);
return state -> {
if (Math.random() > chance) return;
if (state.getPlayer() == null) return;
List<String> replaced = PlaceholderManager.getInstance().parse(
state.getPlayer(),
msg,
@@ -343,6 +345,7 @@ public class ActionManagerImpl implements ActionManager {
private void registerCloseInvAction() {
registerAction("close-inv", (args, chance) -> state -> {
if (Math.random() > chance) return;
if (state.getPlayer() == null) return;
state.getPlayer().closeInventory();
});
}
@@ -352,6 +355,7 @@ public class ActionManagerImpl implements ActionManager {
String text = (String) args;
return state -> {
if (Math.random() > chance) return;
if (state.getPlayer() == null) return;
String parsed = PlaceholderManager.getInstance().parse(state.getPlayer(), text, state.getArgs());
AdventureManagerImpl.getInstance().sendActionbar(state.getPlayer(), parsed);
};
@@ -360,6 +364,7 @@ public class ActionManagerImpl implements ActionManager {
ArrayList<String> texts = ConfigUtils.stringListArgs(args);
return state -> {
if (Math.random() > chance) return;
if (state.getPlayer() == null) return;
String random = texts.get(ThreadLocalRandom.current().nextInt(texts.size()));
random = PlaceholderManager.getInstance().parse(state.getPlayer(), random, state.getArgs());
AdventureManagerImpl.getInstance().sendActionbar(state.getPlayer(), random);
@@ -372,6 +377,7 @@ public class ActionManagerImpl implements ActionManager {
var value = ConfigUtils.getValue(args);
return state -> {
if (Math.random() > chance) return;
if (state.getPlayer() == null) return;
if (CustomCropsPlugin.get().getVersionManager().isSpigot()) {
state.getPlayer().getLocation().getWorld().spawn(state.getPlayer().getLocation(), ExperienceOrb.class, e -> e.setExperience((int) value.get(state.getPlayer())));
} else {
@@ -387,6 +393,7 @@ public class ActionManagerImpl implements ActionManager {
var value = ConfigUtils.getValue(args);
return state -> {
if (Math.random() > chance) return;
if (state.getPlayer() == null) return;
Player player = state.getPlayer();
player.setFoodLevel((int) (player.getFoodLevel() + value.get(player)));
};
@@ -395,6 +402,7 @@ public class ActionManagerImpl implements ActionManager {
var value = ConfigUtils.getValue(args);
return state -> {
if (Math.random() > chance) return;
if (state.getPlayer() == null) return;
Player player = state.getPlayer();
player.setSaturation((float) (player.getSaturation() + value.get(player)));
};
@@ -406,6 +414,7 @@ public class ActionManagerImpl implements ActionManager {
var value = ConfigUtils.getValue(args);
return state -> {
if (Math.random() > chance) return;
if (state.getPlayer() == null) return;
state.getPlayer().giveExp((int) value.get(state.getPlayer()));
AdventureManagerImpl.getInstance().sendSound(state.getPlayer(), Sound.Source.PLAYER, Key.key("minecraft:entity.experience_orb.pickup"), 1, 1);
};
@@ -417,6 +426,7 @@ public class ActionManagerImpl implements ActionManager {
boolean arg = (boolean) args;
return state -> {
if (Math.random() > chance) return;
if (state.getPlayer() == null) return;
state.getPlayer().swingHand(arg ? EquipmentSlot.HAND : EquipmentSlot.OFF_HAND);
};
});
@@ -523,7 +533,7 @@ public class ActionManagerImpl implements ActionManager {
if (r1 < ratio[j]) {
ItemStack drop = plugin.getItemManager().getItemStack(state.getPlayer(), qualityLoots[j]);
if (drop == null || drop.getType() == Material.AIR) return;
if (toInv) {
if (toInv && state.getPlayer() != null) {
ItemUtils.giveItem(state.getPlayer(), drop, 1);
} else {
state.getLocation().getWorld().dropItemNaturally(state.getLocation(), drop);
@@ -563,7 +573,7 @@ public class ActionManagerImpl implements ActionManager {
}
}
itemStack.setAmount(random);
if (toInv) {
if (toInv && state.getPlayer() != null) {
ItemUtils.giveItem(state.getPlayer(), itemStack, random);
} else {
state.getLocation().getWorld().dropItemNaturally(state.getLocation(), itemStack);
@@ -645,20 +655,17 @@ public class ActionManagerImpl implements ActionManager {
plugin.getScheduler().runTaskSync(() -> {
// fire event
if (state.getPlayer() != null) {
CropPlantEvent plantEvent = new CropPlantEvent(state.getPlayer(), state.getItemInHand(), location, crop, 0);
CropPlantEvent plantEvent = new CropPlantEvent(state.getPlayer(), state.getItemInHand(), location, crop, point);
if (EventUtils.fireAndCheckCancel(plantEvent)) {
return;
}
plugin.getItemManager().placeItem(location, crop.getItemCarrier(), crop.getStageItemByPoint(plantEvent.getPoint()), crop.hasRotation() ? CRotation.RANDOM : CRotation.NONE);
plugin.getWorldManager().addCropAt(new MemoryCrop(SimpleLocation.of(location), crop.getKey(), plantEvent.getPoint()), SimpleLocation.of(location));
} else {
plugin.getItemManager().placeItem(location, crop.getItemCarrier(), crop.getStageItemByPoint(point), crop.hasRotation() ? CRotation.RANDOM : CRotation.NONE);
plugin.getWorldManager().addCropAt(new MemoryCrop(SimpleLocation.of(location), crop.getKey(), point), SimpleLocation.of(location));
}
// place the crop
switch (crop.getItemCarrier()) {
case ITEM_FRAME, ITEM_DISPLAY, TRIPWIRE -> plugin.getItemManager().placeItem(location, crop.getItemCarrier(), crop.getStageItemByPoint(point));
default -> {
LogUtils.warn("Unsupported type for crop: " + crop.getItemCarrier().name());
return;
}
}
plugin.getWorldManager().addCropAt(new MemoryCrop(SimpleLocation.of(location), crop.getKey(), point), SimpleLocation.of(location));
}, state.getLocation());
};
} else {
@@ -674,6 +681,10 @@ public class ActionManagerImpl implements ActionManager {
boolean arg = (boolean) (args == null ? true : args);
return state -> {
if (Math.random() > chance) return;
if (state.getPlayer() == null) {
LogUtils.warn("Break action can only be triggered by players");
return;
}
plugin.getScheduler().runTaskSync(() -> {
Optional<CustomCropsBlock> removed = plugin.getWorldManager().getBlockAt(SimpleLocation.of(state.getLocation()));
if (removed.isPresent()) {
@@ -792,6 +803,7 @@ public class ActionManagerImpl implements ActionManager {
return state -> {
if (Math.random() > chance) return;
Player player = state.getPlayer();
if (player == null) return;
ItemStack itemStack = player.getInventory().getItemInMainHand();
itemStack.setAmount(Math.max(0, itemStack.getAmount() + amount));
};
@@ -822,6 +834,7 @@ public class ActionManagerImpl implements ActionManager {
return state -> {
if (Math.random() > chance) return;
Player player = state.getPlayer();
if (player == null) return;
ItemUtils.giveItem(player, Objects.requireNonNull(CustomCropsPlugin.get().getItemManager().getItemStack(player, id)), amount);
};
} else {
@@ -855,6 +868,7 @@ public class ActionManagerImpl implements ActionManager {
var value = ConfigUtils.getValue(args);
return state -> {
if (Math.random() > chance) return;
if (state.getPlayer() == null) return;
VaultHook.getEconomy().depositPlayer(state.getPlayer(), value.get(state.getPlayer()));
};
});
@@ -862,6 +876,7 @@ public class ActionManagerImpl implements ActionManager {
var value = ConfigUtils.getValue(args);
return state -> {
if (Math.random() > chance) return;
if (state.getPlayer() == null) return;
VaultHook.getEconomy().withdrawPlayer(state.getPlayer(), value.get(state.getPlayer()));
};
});
@@ -963,6 +978,7 @@ public class ActionManagerImpl implements ActionManager {
int fadeOut = section.getInt("fade-out", 10);
return state -> {
if (Math.random() > chance) return;
if (state.getPlayer() == null) return;
AdventureManagerImpl.getInstance().sendTitle(
state.getPlayer(),
PlaceholderManager.getInstance().parse(state.getPlayer(), title, state.getArgs()),
@@ -988,6 +1004,7 @@ public class ActionManagerImpl implements ActionManager {
int fadeOut = section.getInt("fade-out", 10);
return state -> {
if (Math.random() > chance) return;
if (state.getPlayer() == null) return;
AdventureManagerImpl.getInstance().sendTitle(
state.getPlayer(),
PlaceholderManager.getInstance().parse(state.getPlayer(), titles.get(ThreadLocalRandom.current().nextInt(titles.size())), state.getArgs()),
@@ -1014,6 +1031,7 @@ public class ActionManagerImpl implements ActionManager {
);
return state -> {
if (Math.random() > chance) return;
if (state.getPlayer() == null) return;
state.getPlayer().addPotionEffect(potionEffect);
};
} else {
@@ -1029,6 +1047,7 @@ public class ActionManagerImpl implements ActionManager {
return state -> {
if (Math.random() > chance) return;
Player player = state.getPlayer();
if (player == null) return;
player.setLevel((int) Math.max(0, player.getLevel() + value.get(state.getPlayer())));
};
});
@@ -1046,6 +1065,7 @@ public class ActionManagerImpl implements ActionManager {
);
return state -> {
if (Math.random() > chance) return;
if (state.getPlayer() == null) return;
AdventureManagerImpl.getInstance().sendSound(state.getPlayer(), sound);
};
} else {
@@ -1122,6 +1142,7 @@ public class ActionManagerImpl implements ActionManager {
String target = section.getString("target");
return state -> {
if (Math.random() > chance) return;
if (state.getPlayer() == null) return;
Optional.ofNullable(plugin.getIntegrationManager().getLevelPlugin(pluginName)).ifPresentOrElse(it -> {
it.addXp(state.getPlayer(), target, value.get(state.getPlayer()));
}, () -> LogUtils.warn("Plugin (" + pluginName + "'s) level is not compatible. Please double check if it's a problem caused by pronunciation."));

View File

@@ -36,6 +36,8 @@ import net.momirealms.customcrops.mechanic.misc.CrowAttackAnimation;
import net.momirealms.customcrops.utils.ClassUtils;
import net.momirealms.customcrops.utils.ConfigUtils;
import org.bukkit.World;
import org.bukkit.block.Block;
import org.bukkit.block.data.type.Farmland;
import org.bukkit.configuration.ConfigurationSection;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@@ -634,6 +636,26 @@ public class ConditionManagerImpl implements ConditionManager {
return worldPot.filter(pot -> pot.getWater() < value).isPresent();
};
});
registerCondition("moisture_more_than", (args) -> {
int value = (int) args;
return block -> {
Block underBlock = block.getLocation().copy().add(0,-1,0).getBukkitLocation().getBlock();
if (underBlock.getBlockData() instanceof Farmland farmland) {
return farmland.getMoisture() > value;
}
return false;
};
});
registerCondition("moisture_less_than", (args) -> {
int value = (int) args;
return block -> {
Block underBlock = block.getLocation().copy().add(0,-1,0).getBukkitLocation().getBlock();
if (underBlock.getBlockData() instanceof Farmland farmland) {
return farmland.getMoisture() < value;
}
return false;
};
});
}
@SuppressWarnings("ResultOfMethodCallIgnored")

View File

@@ -18,12 +18,16 @@
package net.momirealms.customcrops.mechanic.item;
import net.momirealms.customcrops.api.manager.VersionManager;
import net.momirealms.customcrops.api.mechanic.misc.CRotation;
import net.momirealms.customcrops.utils.ConfigUtils;
import net.momirealms.customcrops.utils.DisplayEntityUtils;
import net.momirealms.customcrops.utils.RotationUtils;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.entity.Entity;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.ItemFrame;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
@@ -67,7 +71,7 @@ public interface CustomProvider {
return entities.size() == 0;
}
default void removeAnythingAt(Location location) {
default CRotation removeAnythingAt(Location location) {
if (!removeBlock(location)) {
Collection<Entity> entities = location.getWorld().getNearbyEntities(location.toCenterLocation(), 0.5,0.51,0.5);
entities.removeIf(entity -> {
@@ -75,10 +79,22 @@ public interface CustomProvider {
return type != EntityType.ITEM_FRAME
&& (!VersionManager.isHigherThan1_19_R3() || type != EntityType.ITEM_DISPLAY);
});
if (entities.size() == 0) return CRotation.NONE;
CRotation previousCRotation;
Entity first = entities.stream().findFirst().get();
if (first instanceof ItemFrame itemFrame) {
previousCRotation = RotationUtils.getCRotation(itemFrame.getRotation());
} else if (VersionManager.isHigherThan1_19_R3()) {
previousCRotation = DisplayEntityUtils.getRotation(first);
} else {
previousCRotation = CRotation.NONE;
}
for (Entity entity : entities) {
removeFurniture(entity);
}
return previousCRotation;
}
return CRotation.NONE;
}
default String getSomethingAt(Location location) {

View File

@@ -31,6 +31,7 @@ import net.momirealms.customcrops.api.mechanic.condition.DeathConditions;
import net.momirealms.customcrops.api.mechanic.item.*;
import net.momirealms.customcrops.api.mechanic.item.water.PassiveFillMethod;
import net.momirealms.customcrops.api.mechanic.item.water.PositiveFillMethod;
import net.momirealms.customcrops.api.mechanic.misc.CRotation;
import net.momirealms.customcrops.api.mechanic.misc.image.WaterBar;
import net.momirealms.customcrops.api.mechanic.requirement.State;
import net.momirealms.customcrops.api.mechanic.world.SimpleLocation;
@@ -337,27 +338,24 @@ public class ItemManagerImpl implements ItemManager {
}
@Override
public void placeItem(Location location, ItemCarrier carrier, String id, boolean rotate) {
public void placeItem(Location location, ItemCarrier carrier, String id, CRotation rotate) {
switch (carrier) {
case ITEM_DISPLAY, ITEM_FRAME -> {
Entity entity = customProvider.placeFurniture(location, id);
if (rotate) {
if (entity instanceof ItemFrame frame) {
frame.setRotation(RotationUtils.getRandomRotation());
} else if (entity instanceof ItemDisplay display) {
display.setRotation(RotationUtils.getRandomFloatRotation(), display.getLocation().getPitch());
}
if (rotate == null || rotate == CRotation.NONE) return;
if (entity instanceof ItemFrame frame) {
frame.setRotation(RotationUtils.getBukkitRotation(rotate));
} else if (entity instanceof ItemDisplay display) {
display.setRotation(RotationUtils.getFloatRotation(rotate), display.getLocation().getPitch());
}
}
case TRIPWIRE, NOTE_BLOCK, CHORUS, MUSHROOM -> {
customProvider.placeBlock(location, id);
}
case TRIPWIRE, NOTE_BLOCK, CHORUS, MUSHROOM -> customProvider.placeBlock(location, id);
}
}
@Override
public void removeAnythingAt(Location location) {
customProvider.removeAnythingAt(location);
public CRotation removeAnythingAt(Location location) {
return customProvider.removeAnythingAt(location);
}
@Override
@@ -529,6 +527,11 @@ public class ItemManagerImpl implements ItemManager {
return FunctionResult.PASS;
}
Player player = furnitureWrapper.getPlayer();
// prevent players from rotating the crops
if (player.isSneaking()) {
return FunctionResult.CANCEL_EVENT_AND_RETURN;
}
Location cropLocation = furnitureWrapper.getLocation();
ItemStack itemInHand = furnitureWrapper.getItemInHand();
String itemID = getItemID(itemInHand);
@@ -1661,14 +1664,8 @@ public class ItemManagerImpl implements ItemManager {
return FunctionResult.RETURN;
}
// place the crop
switch (crop.getItemCarrier()) {
case ITEM_FRAME, ITEM_DISPLAY -> customProvider.placeFurniture(seedLocation, crop.getStageItemByPoint(plantEvent.getPoint()));
case TRIPWIRE -> customProvider.placeBlock(seedLocation, crop.getStageItemByPoint(plantEvent.getPoint()));
default -> {
LogUtils.warn("Unsupported type for crop: " + crop.getItemCarrier().name());
return FunctionResult.RETURN;
}
}
this.placeItem(seedLocation, crop.getItemCarrier(), crop.getStageItemByPoint(plantEvent.getPoint()), crop.hasRotation() ? CRotation.RANDOM : CRotation.NONE);
// reduce item
if (player.getGameMode() != GameMode.CREATIVE)
itemInHand.setAmount(itemInHand.getAmount() - 1);
@@ -1690,8 +1687,14 @@ public class ItemManagerImpl implements ItemManager {
if (!(conditionWrapper instanceof InteractWrapper interactWrapper)) {
return FunctionResult.PASS;
}
Location cropLocation = interactWrapper.getLocation().toBlockLocation();
Player player = interactWrapper.getPlayer();
// prevent players from rotating the crops
if (player.isSneaking() && interactWrapper instanceof InteractFurnitureWrapper) {
return FunctionResult.CANCEL_EVENT_AND_RETURN;
}
Location cropLocation = interactWrapper.getLocation().toBlockLocation();
ItemStack itemInHand = interactWrapper.getItemInHand();
State cropState = new State(player, itemInHand, cropLocation);

View File

@@ -23,8 +23,6 @@ import io.th0rgal.oraxen.api.OraxenItems;
import io.th0rgal.oraxen.items.ItemBuilder;
import io.th0rgal.oraxen.mechanics.Mechanic;
import io.th0rgal.oraxen.mechanics.provided.gameplay.furniture.FurnitureMechanic;
import io.th0rgal.oraxen.mechanics.provided.gameplay.noteblock.NoteBlockMechanic;
import io.th0rgal.oraxen.mechanics.provided.gameplay.stringblock.StringBlockMechanic;
import net.momirealms.customcrops.mechanic.item.CustomProvider;
import org.bukkit.Location;
import org.bukkit.Material;

View File

@@ -23,6 +23,7 @@ import net.momirealms.customcrops.api.mechanic.item.Crop;
import net.momirealms.customcrops.api.mechanic.item.Fertilizer;
import net.momirealms.customcrops.api.mechanic.item.Pot;
import net.momirealms.customcrops.api.mechanic.item.Sprinkler;
import net.momirealms.customcrops.api.mechanic.misc.CRotation;
import net.momirealms.customcrops.api.mechanic.requirement.State;
import net.momirealms.customcrops.api.mechanic.world.ChunkCoordinate;
import net.momirealms.customcrops.api.mechanic.world.ChunkPos;
@@ -354,8 +355,8 @@ public class CChunk implements CustomCropsChunk {
String pre = crop.getStageItemByPoint(previousPoint);
String after = crop.getStageItemByPoint(x);
if (pre.equals(after)) return;
CustomCropsPlugin.get().getItemManager().removeAnythingAt(bkLoc);
CustomCropsPlugin.get().getItemManager().placeItem(bkLoc, crop.getItemCarrier(), after, crop.hasRotation());
CRotation CRotation = CustomCropsPlugin.get().getItemManager().removeAnythingAt(bkLoc);
CustomCropsPlugin.get().getItemManager().placeItem(bkLoc, crop.getItemCarrier(), after, CRotation);
}
@Override

View File

@@ -458,12 +458,12 @@ public class BukkitWorldAdaptor extends AbstractWorldAdaptor {
// read date and season
YamlConfiguration data = YamlConfiguration.loadConfiguration(leagcyFile);
try {
Season season = Season.valueOf(data.getString("season"));
Season season = Season.valueOf(data.getString("season", "SPRING"));
if (cWorld != null)
cWorld.setInfoData(new WorldInfoData(season, data.getInt("date", 1)));
world.getPersistentDataContainer().set(key, PersistentDataType.STRING,
gson.toJson(new WorldInfoData(season, data.getInt("date", 1))));
} catch (IllegalArgumentException e) {
} catch (Exception e) {
if (cWorld != null)
cWorld.setInfoData(WorldInfoData.empty());
}

View File

@@ -23,7 +23,6 @@ import com.infernalsuite.aswm.api.events.LoadSlimeWorldEvent;
import com.infernalsuite.aswm.api.world.SlimeWorld;
import net.momirealms.customcrops.api.CustomCropsPlugin;
import net.momirealms.customcrops.api.manager.WorldManager;
import net.momirealms.customcrops.api.mechanic.world.AbstractWorldAdaptor;
import net.momirealms.customcrops.api.mechanic.world.ChunkCoordinate;
import net.momirealms.customcrops.api.mechanic.world.ChunkPos;
import net.momirealms.customcrops.api.mechanic.world.CustomCropsBlock;
@@ -37,12 +36,11 @@ import net.momirealms.customcrops.scheduler.task.TickTask;
import org.bukkit.Bukkit;
import org.bukkit.World;
import org.bukkit.event.EventHandler;
import org.bukkit.event.world.WorldLoadEvent;
import org.bukkit.event.world.WorldUnloadEvent;
import org.bukkit.persistence.PersistentDataType;
import java.io.*;
import java.util.*;
import java.util.HashSet;
import java.util.Map;
import java.util.Optional;
import java.util.PriorityQueue;
import java.util.concurrent.ConcurrentHashMap;
public class SlimeWorldAdaptor extends BukkitWorldAdaptor {

View File

@@ -28,6 +28,7 @@ import net.momirealms.customcrops.api.mechanic.item.Crop;
import net.momirealms.customcrops.api.mechanic.item.Fertilizer;
import net.momirealms.customcrops.api.mechanic.item.ItemType;
import net.momirealms.customcrops.api.mechanic.item.fertilizer.SpeedGrow;
import net.momirealms.customcrops.api.mechanic.misc.CRotation;
import net.momirealms.customcrops.api.mechanic.requirement.State;
import net.momirealms.customcrops.api.mechanic.world.SimpleLocation;
import net.momirealms.customcrops.api.mechanic.world.level.AbstractCustomCropsBlock;
@@ -173,8 +174,8 @@ public class MemoryCrop extends AbstractCustomCropsBlock implements WorldCrop {
}
}
if (pre.equals(after)) return;
CustomCropsPlugin.get().getItemManager().removeAnythingAt(bukkitLocation);
CustomCropsPlugin.get().getItemManager().placeItem(bukkitLocation, crop.getItemCarrier(), after, crop.hasRotation());
CRotation CRotation = CustomCropsPlugin.get().getItemManager().removeAnythingAt(bukkitLocation);
CustomCropsPlugin.get().getItemManager().placeItem(bukkitLocation, crop.getItemCarrier(), after, CRotation);
}, bukkitLocation);
}
}

View File

@@ -0,0 +1,15 @@
package net.momirealms.customcrops.utils;
import net.momirealms.customcrops.api.mechanic.misc.CRotation;
import org.bukkit.entity.Entity;
import org.bukkit.entity.ItemDisplay;
public class DisplayEntityUtils {
public static CRotation getRotation(Entity entity) {
if (entity instanceof ItemDisplay itemDisplay) {
return RotationUtils.getCRotation(itemDisplay.getLocation().getYaw());
}
return CRotation.NONE;
}
}

View File

@@ -1,29 +0,0 @@
/*
* 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.utils;
import java.util.concurrent.ThreadLocalRandom;
public class RandomUtils {
private static final ThreadLocalRandom randomSource = ThreadLocalRandom.current();
public static int getRandomInt(int from, int to) {
return randomSource.nextInt(from, to);
}
}

View File

@@ -17,6 +17,7 @@
package net.momirealms.customcrops.utils;
import net.momirealms.customcrops.api.mechanic.misc.CRotation;
import org.bukkit.Rotation;
import java.util.Random;
@@ -26,11 +27,73 @@ public class RotationUtils {
private static final Rotation[] rotationsI = {Rotation.NONE, Rotation.FLIPPED, Rotation.CLOCKWISE, Rotation.COUNTER_CLOCKWISE};
private static final float[] rotationsF = {0f, 90f, 180f, -90f};
public static Rotation getRandomRotation() {
public static Rotation getRandomBukkitRotation() {
return rotationsI[new Random().nextInt(4)];
}
public static float getRandomFloatRotation() {
return rotationsF[new Random().nextInt(4)];
}
public static float getFloatRotation(CRotation cRotation) {
if (cRotation == CRotation.RANDOM) {
return getRandomFloatRotation();
}
return cRotation.getYaw();
}
public static Rotation getBukkitRotation(CRotation cRotation) {
switch (cRotation) {
case RANDOM -> {
return getRandomBukkitRotation();
}
case EAST -> {
return Rotation.COUNTER_CLOCKWISE;
}
case WEST -> {
return Rotation.CLOCKWISE;
}
case NORTH -> {
return Rotation.FLIPPED;
}
default -> {
return Rotation.NONE;
}
}
}
public static CRotation getCRotation(Rotation rotation) {
switch (rotation) {
default -> {
return CRotation.NONE;
}
case CLOCKWISE -> {
return CRotation.WEST;
}
case COUNTER_CLOCKWISE -> {
return CRotation.EAST;
}
case FLIPPED -> {
return CRotation.NORTH;
}
}
}
public static CRotation getCRotation(float yaw) {
yaw = Math.abs(yaw);
switch ((int) (yaw/90)) {
case 1 -> {
return CRotation.WEST;
}
case 2 -> {
return CRotation.NORTH;
}
case 3 -> {
return CRotation.EAST;
}
default -> {
return CRotation.SOUTH;
}
}
}
}