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

[API] Improved API

This commit is contained in:
XiaoMoMi
2024-04-22 04:49:48 +08:00
parent 6de266a1e5
commit 6dd75c6c40
53 changed files with 1626 additions and 181 deletions

View File

@@ -81,7 +81,7 @@ public interface CustomProvider {
CRotation previousCRotation;
Entity first = entities.stream().findFirst().get();
if (first instanceof ItemFrame itemFrame) {
previousCRotation = RotationUtils.getCRotation(itemFrame.getRotation());
previousCRotation = CRotation.getByRotation(itemFrame.getRotation());
} else if (VersionManager.isHigherThan1_19_R3()) {
previousCRotation = DisplayEntityUtils.getRotation(first);
} else {
@@ -109,4 +109,27 @@ public interface CustomProvider {
}
return "AIR";
}
default CRotation getRotation(Location location) {
if (location.getBlock().getType() == Material.AIR) {
Collection<Entity> entities = location.getWorld().getNearbyEntities(LocationUtils.toCenterLocation(location), 0.5,0.51,0.5);
entities.removeIf(entity -> {
EntityType type = entity.getType();
return type != EntityType.ITEM_FRAME
&& (!VersionManager.isHigherThan1_19_R3() || type != EntityType.ITEM_DISPLAY);
});
if (entities.size() == 0) return CRotation.NONE;
CRotation rotation;
Entity first = entities.stream().findFirst().get();
if (first instanceof ItemFrame itemFrame) {
rotation = CRotation.getByRotation(itemFrame.getRotation());
} else if (VersionManager.isHigherThan1_19_R3()) {
rotation = DisplayEntityUtils.getRotation(first);
} else {
rotation = CRotation.NONE;
}
return rotation;
}
return CRotation.NONE;
}
}

View File

@@ -372,6 +372,11 @@ public class ItemManagerImpl implements ItemManager {
return customProvider.removeAnythingAt(location);
}
@Override
public CRotation getRotation(Location location) {
return customProvider.getRotation(location);
}
@Override
public WateringCan getWateringCanByID(@NotNull String id) {
return id2WateringCanMap.get(id);
@@ -1117,7 +1122,7 @@ public class ItemManagerImpl implements ItemManager {
if (!wateringCan.isInfinite()) {
PositiveFillMethod[] methods = wateringCan.getPositiveFillMethods();
for (PositiveFillMethod method : methods) {
if (method.getId().equals(clickedFurnitureID)) {
if (method.getID().equals(clickedFurnitureID)) {
if (method.canFill(state)) {
// fire the event
WateringCanFillEvent fillEvent = new WateringCanFillEvent(player, itemInHand, location, wateringCan, method);
@@ -1176,7 +1181,7 @@ public class ItemManagerImpl implements ItemManager {
int water = wateringCan.getCurrentWater(itemInHand);
PositiveFillMethod[] methods = wateringCan.getPositiveFillMethods();
for (PositiveFillMethod method : methods) {
if (method.getId().equals(blockID)) {
if (method.getID().equals(blockID)) {
if (method.canFill(state)) {
if (water < wateringCan.getStorage()) {
// fire the event
@@ -1230,7 +1235,7 @@ public class ItemManagerImpl implements ItemManager {
int water = wateringCan.getCurrentWater(itemInHand);
PositiveFillMethod[] methods = wateringCan.getPositiveFillMethods();
for (PositiveFillMethod method : methods) {
if (method.getId().equals(blockID)) {
if (method.getID().equals(blockID)) {
if (method.canFill(state)) {
if (water < wateringCan.getStorage()) {
// fire the event

View File

@@ -26,6 +26,7 @@ import net.momirealms.customcrops.api.mechanic.item.Crop;
import net.momirealms.customcrops.api.mechanic.item.ItemCarrier;
import net.momirealms.customcrops.api.mechanic.requirement.Requirement;
import net.momirealms.customcrops.mechanic.item.AbstractEventItem;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.ArrayList;
@@ -143,6 +144,7 @@ public class CropConfig extends AbstractEventItem implements Crop {
return point2StageConfigMap.get(point);
}
@NotNull
@Override
public String getStageItemByPoint(int point) {
if (point >= 0) {

View File

@@ -206,7 +206,7 @@ public class CChunk implements CustomCropsChunk {
}
}
case POT -> {
((WorldPot) block).tickWater(this);
((WorldPot) block).tickWater();
if (setting.randomTickPot()) {
block.tick(setting.getTickPotInterval(), offline);
}
@@ -277,7 +277,7 @@ public class CChunk implements CustomCropsChunk {
}
@Override
public void addWaterToSprinkler(Sprinkler sprinkler, SimpleLocation location, int amount) {
public void addWaterToSprinkler(Sprinkler sprinkler, int amount, SimpleLocation location) {
Optional<WorldSprinkler> optionalSprinkler = getSprinklerAt(location);
if (optionalSprinkler.isEmpty()) {
addBlockAt(new MemorySprinkler(location, sprinkler.getKey(), amount), location);
@@ -314,7 +314,7 @@ public class CChunk implements CustomCropsChunk {
}
@Override
public void addWaterToPot(Pot pot, SimpleLocation location, int amount) {
public void addWaterToPot(Pot pot, int amount, SimpleLocation location) {
Optional<WorldPot> optionalWorldPot = getPotAt(location);
if (optionalWorldPot.isEmpty()) {
MemoryPot memoryPot = new MemoryPot(location, pot.getKey());
@@ -365,7 +365,7 @@ public class CChunk implements CustomCropsChunk {
}
@Override
public void addPointToCrop(Crop crop, SimpleLocation location, int points) {
public void addPointToCrop(Crop crop, int points, SimpleLocation location) {
if (points <= 0) return;
Optional<WorldCrop> cropData = getCropAt(location);
if (cropData.isEmpty()) {
@@ -415,52 +415,72 @@ public class CChunk implements CustomCropsChunk {
}
@Override
public void removeSprinklerAt(SimpleLocation location) {
public WorldSprinkler removeSprinklerAt(SimpleLocation location) {
CustomCropsBlock removed = removeBlockAt(location);
if (removed == null) {
CustomCropsPlugin.get().debug("Failed to remove sprinkler from " + location + " because sprinkler doesn't exist.");
} else if (!(removed instanceof WorldSprinkler)) {
return null;
} else if (!(removed instanceof WorldSprinkler worldSprinkler)) {
CustomCropsPlugin.get().debug("Removed sprinkler from " + location + " but the previous block type is " + removed.getType().name());
return null;
} else {
return worldSprinkler;
}
}
@Override
public void removePotAt(SimpleLocation location) {
public WorldPot removePotAt(SimpleLocation location) {
CustomCropsBlock removed = removeBlockAt(location);
if (removed == null) {
CustomCropsPlugin.get().debug("Failed to remove pot from " + location + " because pot doesn't exist.");
} else if (!(removed instanceof WorldPot)) {
return null;
} else if (!(removed instanceof WorldPot worldPot)) {
CustomCropsPlugin.get().debug("Removed pot from " + location + " but the previous block type is " + removed.getType().name());
return null;
} else {
return worldPot;
}
}
@Override
public void removeCropAt(SimpleLocation location) {
public WorldCrop removeCropAt(SimpleLocation location) {
CustomCropsBlock removed = removeBlockAt(location);
if (removed == null) {
CustomCropsPlugin.get().debug("Failed to remove crop from " + location + " because crop doesn't exist.");
} else if (!(removed instanceof WorldCrop)) {
return null;
} else if (!(removed instanceof WorldCrop worldCrop)) {
CustomCropsPlugin.get().debug("Removed crop from " + location + " but the previous block type is " + removed.getType().name());
return null;
} else {
return worldCrop;
}
}
@Override
public void removeGlassAt(SimpleLocation location) {
public WorldGlass removeGlassAt(SimpleLocation location) {
CustomCropsBlock removed = removeBlockAt(location);
if (removed == null) {
CustomCropsPlugin.get().debug("Failed to remove glass from " + location + " because glass doesn't exist.");
} else if (!(removed instanceof WorldGlass)) {
return null;
} else if (!(removed instanceof WorldGlass worldGlass)) {
CustomCropsPlugin.get().debug("Removed glass from " + location + " but the previous block type is " + removed.getType().name());
return null;
} else {
return worldGlass;
}
}
@Override
public void removeScarecrowAt(SimpleLocation location) {
public WorldScarecrow removeScarecrowAt(SimpleLocation location) {
CustomCropsBlock removed = removeBlockAt(location);
if (removed == null) {
CustomCropsPlugin.get().debug("Failed to remove scarecrow from " + location + " because scarecrow doesn't exist.");
} else if (!(removed instanceof WorldScarecrow)) {
return null;
} else if (!(removed instanceof WorldScarecrow worldScarecrow)) {
CustomCropsPlugin.get().debug("Removed scarecrow from " + location + " but the previous block type is " + removed.getType().name());
return null;
} else {
return worldScarecrow;
}
}

View File

@@ -39,6 +39,7 @@ import net.momirealms.customcrops.api.util.LogUtils;
import net.momirealms.customcrops.util.EventUtils;
import org.bukkit.Bukkit;
import org.bukkit.World;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.lang.ref.WeakReference;
@@ -217,6 +218,7 @@ public class CWorld implements CustomCropsWorld {
});
}
@NotNull
@Override
public String getWorldName() {
return worldName;
@@ -359,10 +361,10 @@ public class CWorld implements CustomCropsWorld {
}
@Override
public void addWaterToSprinkler(Sprinkler sprinkler, SimpleLocation location, int amount) {
public void addWaterToSprinkler(Sprinkler sprinkler, int amount, SimpleLocation location) {
Optional<CustomCropsChunk> chunk = getOrCreateLoadedChunkAt(location.getChunkPos());
if (chunk.isPresent()) {
chunk.get().addWaterToSprinkler(sprinkler, location, amount);
chunk.get().addWaterToSprinkler(sprinkler, amount, location);
} else {
LogUtils.warn("Invalid operation: Adding water to sprinkler in a not generated chunk");
}
@@ -379,10 +381,10 @@ public class CWorld implements CustomCropsWorld {
}
@Override
public void addWaterToPot(Pot pot, SimpleLocation location, int amount) {
public void addWaterToPot(Pot pot, int amount, SimpleLocation location) {
Optional<CustomCropsChunk> chunk = getOrCreateLoadedChunkAt(location.getChunkPos());
if (chunk.isPresent()) {
chunk.get().addWaterToPot(pot, location, amount);
chunk.get().addWaterToPot(pot, amount, location);
} else {
LogUtils.warn("Invalid operation: Adding water to pot in a not generated chunk");
}
@@ -419,10 +421,10 @@ public class CWorld implements CustomCropsWorld {
}
@Override
public void addPointToCrop(Crop crop, SimpleLocation location, int points) {
public void addPointToCrop(Crop crop, int points, SimpleLocation location) {
Optional<CustomCropsChunk> chunk = getOrCreateLoadedChunkAt(location.getChunkPos());
if (chunk.isPresent()) {
chunk.get().addPointToCrop(crop, location, points);
chunk.get().addPointToCrop(crop, points, location);
} else {
LogUtils.warn("Invalid operation: Adding point to crop in a not generated chunk");
}
@@ -449,52 +451,57 @@ public class CWorld implements CustomCropsWorld {
}
@Override
public void removeSprinklerAt(SimpleLocation location) {
public WorldSprinkler removeSprinklerAt(SimpleLocation location) {
Optional<CustomCropsChunk> chunk = getLoadedChunkAt(location.getChunkPos());
if (chunk.isPresent()) {
chunk.get().removeSprinklerAt(location);
return chunk.get().removeSprinklerAt(location);
} else {
LogUtils.warn("Invalid operation: Removing sprinkler from an unloaded chunk");
LogUtils.warn("Invalid operation: Removing sprinkler from an unloaded/empty chunk");
return null;
}
}
@Override
public void removePotAt(SimpleLocation location) {
public WorldPot removePotAt(SimpleLocation location) {
Optional<CustomCropsChunk> chunk = getLoadedChunkAt(location.getChunkPos());
if (chunk.isPresent()) {
chunk.get().removePotAt(location);
return chunk.get().removePotAt(location);
} else {
LogUtils.warn("Invalid operation: Removing pot from an unloaded chunk");
LogUtils.warn("Invalid operation: Removing pot from an unloaded/empty chunk");
return null;
}
}
@Override
public void removeCropAt(SimpleLocation location) {
public WorldCrop removeCropAt(SimpleLocation location) {
Optional<CustomCropsChunk> chunk = getLoadedChunkAt(location.getChunkPos());
if (chunk.isPresent()) {
chunk.get().removeCropAt(location);
return chunk.get().removeCropAt(location);
} else {
LogUtils.warn("Invalid operation: Removing crop from an unloaded chunk");
LogUtils.warn("Invalid operation: Removing crop from an unloaded/empty chunk");
return null;
}
}
@Override
public void removeGlassAt(SimpleLocation location) {
public WorldGlass removeGlassAt(SimpleLocation location) {
Optional<CustomCropsChunk> chunk = getLoadedChunkAt(location.getChunkPos());
if (chunk.isPresent()) {
chunk.get().removeGlassAt(location);
return chunk.get().removeGlassAt(location);
} else {
LogUtils.warn("Invalid operation: Removing glass from an unloaded chunk");
LogUtils.warn("Invalid operation: Removing glass from an unloaded/empty chunk");
return null;
}
}
@Override
public void removeScarecrowAt(SimpleLocation location) {
public WorldScarecrow removeScarecrowAt(SimpleLocation location) {
Optional<CustomCropsChunk> chunk = getLoadedChunkAt(location.getChunkPos());
if (chunk.isPresent()) {
chunk.get().removeScarecrowAt(location);
return chunk.get().removeScarecrowAt(location);
} else {
LogUtils.warn("Invalid operation: Removing scarecrow from an unloaded chunk");
LogUtils.warn("Invalid operation: Removing scarecrow from an unloaded/empty chunk");
return null;
}
}
@@ -504,7 +511,7 @@ public class CWorld implements CustomCropsWorld {
if (chunk.isPresent()) {
return chunk.get().removeBlockAt(location);
} else {
LogUtils.warn("Invalid operation: Removing anything from an unloaded chunk");
LogUtils.warn("Invalid operation: Removing anything from an unloaded/empty chunk");
return null;
}
}

View File

@@ -321,7 +321,7 @@ public class WorldManagerImpl implements WorldManager, Listener {
LogUtils.warn("Unsupported operation: Adding water to sprinkler in unloaded world " + location);
return;
}
cWorld.addWaterToSprinkler(sprinkler, location, amount);
cWorld.addWaterToSprinkler(sprinkler, amount, location);
}
@Override
@@ -342,7 +342,7 @@ public class WorldManagerImpl implements WorldManager, Listener {
LogUtils.warn("Unsupported operation: Adding water to pot in unloaded world " + location);
return;
}
cWorld.addWaterToPot(pot, location, amount);
cWorld.addWaterToPot(pot, amount, location);
}
@Override
@@ -382,7 +382,7 @@ public class WorldManagerImpl implements WorldManager, Listener {
LogUtils.warn("Unsupported operation: Adding point to crop in unloaded world " + location);
return;
}
cWorld.addPointToCrop(crop, location, points);
cWorld.addPointToCrop(crop, points, location);
}
@Override
@@ -406,53 +406,53 @@ public class WorldManagerImpl implements WorldManager, Listener {
}
@Override
public void removeSprinklerAt(@NotNull SimpleLocation location) {
public WorldSprinkler removeSprinklerAt(@NotNull SimpleLocation location) {
CWorld cWorld = loadedWorlds.get(location.getWorldName());
if (cWorld == null) {
LogUtils.warn("Unsupported operation: Removing sprinkler from unloaded world " + location);
return;
return null;
}
cWorld.removeSprinklerAt(location);
return cWorld.removeSprinklerAt(location);
}
@Override
public void removePotAt(@NotNull SimpleLocation location) {
public WorldPot removePotAt(@NotNull SimpleLocation location) {
CWorld cWorld = loadedWorlds.get(location.getWorldName());
if (cWorld == null) {
LogUtils.warn("Unsupported operation: Removing pot from unloaded world " + location);
return;
return null;
}
cWorld.removePotAt(location);
return cWorld.removePotAt(location);
}
@Override
public void removeCropAt(@NotNull SimpleLocation location) {
public WorldCrop removeCropAt(@NotNull SimpleLocation location) {
CWorld cWorld = loadedWorlds.get(location.getWorldName());
if (cWorld == null) {
LogUtils.warn("Unsupported operation: Removing crop from unloaded world " + location);
return;
return null;
}
cWorld.removeCropAt(location);
return cWorld.removeCropAt(location);
}
@Override
public void removeGlassAt(@NotNull SimpleLocation location) {
public WorldGlass removeGlassAt(@NotNull SimpleLocation location) {
CWorld cWorld = loadedWorlds.get(location.getWorldName());
if (cWorld == null) {
LogUtils.warn("Unsupported operation: Removing glass from unloaded world " + location);
return;
return null;
}
cWorld.removeGlassAt(location);
return cWorld.removeGlassAt(location);
}
@Override
public void removeScarecrowAt(@NotNull SimpleLocation location) {
public WorldScarecrow removeScarecrowAt(@NotNull SimpleLocation location) {
CWorld cWorld = loadedWorlds.get(location.getWorldName());
if (cWorld == null) {
LogUtils.warn("Unsupported operation: Removing scarecrow from unloaded world " + location);
return;
return null;
}
cWorld.removeScarecrowAt(location);
return cWorld.removeScarecrowAt(location);
}
@Override

View File

@@ -35,6 +35,7 @@ import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.block.Block;
import org.bukkit.block.data.Waterlogged;
import org.jetbrains.annotations.NotNull;
import java.util.Objects;
@@ -95,7 +96,7 @@ public class MemoryPot extends AbstractCustomCropsBlock implements WorldPot {
}
@Override
public void setFertilizer(Fertilizer fertilizer) {
public void setFertilizer(@NotNull Fertilizer fertilizer) {
setData("fertilizer", new StringTag("fertilizer", fertilizer.getKey()));
setData("fertilizer-times", new IntTag("fertilizer-times", fertilizer.getTimes()));
}
@@ -122,7 +123,7 @@ public class MemoryPot extends AbstractCustomCropsBlock implements WorldPot {
}
@Override
public void tickWater(CustomCropsChunk chunk) {
public void tickWater() {
Pot pot = getConfig();
if (pot == null) {
LogUtils.warn("Found a pot without config at " + getLocation() + ". Try removing the data.");

View File

@@ -8,7 +8,7 @@ public class DisplayEntityUtils {
public static CRotation getRotation(Entity entity) {
if (entity instanceof ItemDisplay itemDisplay) {
return RotationUtils.getCRotation(itemDisplay.getLocation().getYaw());
return CRotation.getByYaw(itemDisplay.getLocation().getYaw());
}
return CRotation.NONE;
}

View File

@@ -61,39 +61,4 @@ public class RotationUtils {
}
}
}
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;
}
}
}
}