9
0
mirror of https://github.com/Xiao-MoMi/Custom-Crops.git synced 2025-12-26 10:29:10 +00:00

fix item detection

This commit is contained in:
XiaoMoMi
2024-03-29 13:09:20 +08:00
parent 3e7633a4b0
commit c82244188e
12 changed files with 53 additions and 34 deletions

View File

@@ -46,6 +46,6 @@ public class MMOItemsItemImpl implements ItemLibrary {
public String getItemID(ItemStack itemStack) {
NBTItem nbtItem = new NBTItem(itemStack);
if (!nbtItem.hasTag("MMOITEMS_ITEM_ID")) return null;
return nbtItem.getString("MMOITEMS_ITEM_ID");
return nbtItem.getString("MMOITEMS_ITEM_TYPE") + ":" + nbtItem.getString("MMOITEMS_ITEM_ID");
}
}

View File

@@ -19,6 +19,7 @@ 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.api.util.LocationUtils;
import net.momirealms.customcrops.utils.ConfigUtils;
import net.momirealms.customcrops.utils.DisplayEntityUtils;
import net.momirealms.customcrops.utils.RotationUtils;
@@ -65,7 +66,7 @@ public interface CustomProvider {
Block block = location.getBlock();
if (block.getType() != Material.AIR)
return false;
Location center = location.toCenterLocation();
Location center = LocationUtils.toCenterLocation(location);
Collection<Entity> entities = center.getWorld().getNearbyEntities(center, 0.5,0.51,0.5);
entities.removeIf(entity -> entity instanceof Player);
return entities.size() == 0;
@@ -73,7 +74,7 @@ public interface CustomProvider {
default CRotation removeAnythingAt(Location location) {
if (!removeBlock(location)) {
Collection<Entity> entities = location.getWorld().getNearbyEntities(location.toCenterLocation(), 0.5,0.51,0.5);
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
@@ -102,7 +103,7 @@ public interface CustomProvider {
if (block.getType() != Material.AIR) {
return getBlockID(block);
} else {
Collection<Entity> entities = location.getWorld().getNearbyEntities(location.toCenterLocation(), 0.5,0.5,0.5);
Collection<Entity> entities = location.getWorld().getNearbyEntities(location.toCenterLocation(), 0.5,0.51,0.5);
for (Entity entity : entities) {
if (isFurniture(entity)) {
return getEntityID(entity);

View File

@@ -38,6 +38,7 @@ import net.momirealms.customcrops.api.mechanic.requirement.State;
import net.momirealms.customcrops.api.mechanic.world.CustomCropsBlock;
import net.momirealms.customcrops.api.mechanic.world.SimpleLocation;
import net.momirealms.customcrops.api.mechanic.world.level.*;
import net.momirealms.customcrops.api.util.LocationUtils;
import net.momirealms.customcrops.api.util.LogUtils;
import net.momirealms.customcrops.mechanic.item.custom.AbstractCustomListener;
import net.momirealms.customcrops.mechanic.item.custom.crucible.CrucibleListener;
@@ -124,13 +125,13 @@ public class ItemManagerImpl implements ItemManager {
this.item2FertilizerMap = new HashMap<>();
this.stage2CropStageMap = new HashMap<>();
this.deadCrops = new HashSet<>();
if (Bukkit.getPluginManager().isPluginEnabled("Oraxen")) {
if (Bukkit.getPluginManager().getPlugin("Oraxen") != null) {
listener = new OraxenListener(this);
customProvider = new OraxenProvider();
} else if (Bukkit.getPluginManager().isPluginEnabled("ItemsAdder")) {
} else if (Bukkit.getPluginManager().getPlugin("ItemsAdder") != null) {
listener = new ItemsAdderListener(this);
customProvider = new ItemsAdderProvider();
} else if (Bukkit.getPluginManager().isPluginEnabled("MythicCrucible")) {
} else if (Bukkit.getPluginManager().getPlugin("MythicCrucible") != null) {
listener = new CrucibleListener(this);
customProvider = new CrucibleProvider();
} else {
@@ -221,7 +222,7 @@ public class ItemManagerImpl implements ItemManager {
for (ItemLibrary library : itemDetectionArray) {
id = library.getItemID(itemStack);
if (id != null)
return id;
return library.identification() + ":" + id;
}
}
return itemStack.getType().name();
@@ -1821,7 +1822,7 @@ public class ItemManagerImpl implements ItemManager {
}
Player player = interactWrapper.getPlayer();
Location cropLocation = interactWrapper.getLocation().toBlockLocation();
Location cropLocation = LocationUtils.toBlockLocation(interactWrapper.getLocation());
ItemStack itemInHand = interactWrapper.getItemInHand();
State cropState = new State(player, itemInHand, cropLocation);
@@ -1928,7 +1929,7 @@ public class ItemManagerImpl implements ItemManager {
return FunctionResult.PASS;
}
Player player = breakWrapper.getPlayer();
Location cropLocation = breakWrapper.getLocation().toBlockLocation();
Location cropLocation = LocationUtils.toBlockLocation(breakWrapper.getLocation());
State state = new State(player, breakWrapper.getItemInHand(), cropLocation);
// check crop break requirements
if (!RequirementManager.isRequirementMet(state, crop.getBreakRequirements())) {

View File

@@ -76,8 +76,6 @@ public class CrucibleProvider implements CustomProvider {
@Override
public Entity placeFurniture(Location location, String id) {
Location center = location.toCenterLocation();
center.setY(center.getBlockY());
Optional<CrucibleItem> optionalCI = itemManager.getItem(id);
if (optionalCI.isPresent()) {
return optionalCI.get().getFurnitureData().placeFrame(location.getBlock(), BlockFace.UP, 0f, null);
@@ -101,9 +99,7 @@ public class CrucibleProvider implements CustomProvider {
@Override
public String getItemID(ItemStack itemStack) {
Optional<CrucibleItem> optionalCI = itemManager.getItem(itemStack);
if (optionalCI.isEmpty()) return itemStack.getType().name();
else return optionalCI.get().getInternalName();
return itemManager.getItem(itemStack).map(CrucibleItem::getInternalName).orElse(null);
}
@Override

View File

@@ -20,6 +20,7 @@ package net.momirealms.customcrops.mechanic.item.custom.itemsadder;
import dev.lone.itemsadder.api.CustomBlock;
import dev.lone.itemsadder.api.CustomFurniture;
import dev.lone.itemsadder.api.CustomStack;
import net.momirealms.customcrops.api.util.LocationUtils;
import net.momirealms.customcrops.api.util.LogUtils;
import net.momirealms.customcrops.mechanic.item.CustomProvider;
import org.bukkit.Location;
@@ -52,8 +53,6 @@ public class ItemsAdderProvider implements CustomProvider {
@Override
public Entity placeFurniture(Location location, String id) {
try {
Location center = location.toCenterLocation();
center.setY(center.getBlockY());
CustomFurniture furniture = CustomFurniture.spawnPreciseNonSolid(id, location);
if (furniture == null) return null;
return furniture.getEntity();
@@ -82,7 +81,7 @@ public class ItemsAdderProvider implements CustomProvider {
public String getItemID(ItemStack itemInHand) {
CustomStack customStack = CustomStack.byItemStack(itemInHand);
if (customStack == null) {
return itemInHand.getType().name();
return null;
}
return customStack.getNamespacedID();
}

View File

@@ -24,6 +24,7 @@ import io.th0rgal.oraxen.api.events.noteblock.OraxenNoteBlockBreakEvent;
import io.th0rgal.oraxen.api.events.noteblock.OraxenNoteBlockPlaceEvent;
import io.th0rgal.oraxen.api.events.stringblock.OraxenStringBlockBreakEvent;
import io.th0rgal.oraxen.api.events.stringblock.OraxenStringBlockPlaceEvent;
import net.momirealms.customcrops.api.util.LocationUtils;
import net.momirealms.customcrops.mechanic.item.ItemManagerImpl;
import net.momirealms.customcrops.mechanic.item.custom.AbstractCustomListener;
import org.bukkit.event.EventHandler;
@@ -88,7 +89,7 @@ public class OraxenListener extends AbstractCustomListener {
public void onBreakFurniture(OraxenFurnitureBreakEvent event) {
super.onBreakFurniture(
event.getPlayer(),
event.getBaseEntity().getLocation().toBlockLocation(),
LocationUtils.toBlockLocation(event.getBaseEntity().getLocation()),
event.getMechanic().getItemID(),
event
);
@@ -98,7 +99,7 @@ public class OraxenListener extends AbstractCustomListener {
public void onInteractFurniture(OraxenFurnitureInteractEvent event) {
super.onInteractFurniture(
event.getPlayer(),
event.getBaseEntity().getLocation().toBlockLocation(),
LocationUtils.toBlockLocation(event.getBaseEntity().getLocation()),
event.getMechanic().getItemID(),
event.getBaseEntity(),
event

View File

@@ -54,8 +54,6 @@ public class OraxenProvider implements CustomProvider {
@Override
public Entity placeFurniture(Location location, String id) {
Location center = location.toCenterLocation();
center.setY(center.getBlockY());
Entity entity = OraxenFurniture.place(id, location, Rotation.NONE, BlockFace.UP);
if (entity == null) {
LogUtils.warn("Furniture(" + id +") doesn't exist in Oraxen configs. Please double check if that furniture exists.");
@@ -79,11 +77,7 @@ public class OraxenProvider implements CustomProvider {
@Override
public String getItemID(ItemStack itemStack) {
String id = OraxenItems.getIdByItem(itemStack);
if (id == null) {
return itemStack.getType().name();
}
return id;
return OraxenItems.getIdByItem(itemStack);
}
@Override

View File

@@ -37,6 +37,7 @@ import net.momirealms.customcrops.api.scheduler.CancellableTask;
import net.momirealms.customcrops.api.scheduler.Scheduler;
import net.momirealms.customcrops.api.util.LogUtils;
import net.momirealms.customcrops.utils.EventUtils;
import org.bukkit.Bukkit;
import org.bukkit.World;
import org.jetbrains.annotations.Nullable;
@@ -51,7 +52,7 @@ import java.util.concurrent.TimeUnit;
public class CWorld implements CustomCropsWorld {
private final WorldManager worldManager;
private final WeakReference<World> world;
private WeakReference<World> world;
private final ConcurrentHashMap<ChunkPos, CChunk> loadedChunks;
private final ConcurrentHashMap<ChunkPos, CChunk> lazyChunks;
private final ConcurrentHashMap<RegionPos, CRegion> loadedRegions;
@@ -124,7 +125,7 @@ public class CWorld implements CustomCropsWorld {
if (VersionManager.folia()) {
Scheduler scheduler = CustomCropsPlugin.get().getScheduler();
for (CChunk chunk : loadedChunks.values()) {
scheduler.runTaskSync(chunk::secondTimer,world.get(), chunk.getChunkPos().x(), chunk.getChunkPos().z());
scheduler.runTaskSync(chunk::secondTimer, getWorld(), chunk.getChunkPos().x(), chunk.getChunkPos().z());
}
} else {
for (CChunk chunk : loadedChunks.values()) {
@@ -151,7 +152,7 @@ public class CWorld implements CustomCropsWorld {
}
private void updateSeasonAndDate() {
World bukkitWorld = world.get();
World bukkitWorld = getWorld();
if (bukkitWorld == null) {
LogUtils.severe(String.format("World %s unloaded unexpectedly. Stop ticking task...", worldName));
this.cancelTick();
@@ -201,7 +202,13 @@ public class CWorld implements CustomCropsWorld {
@Nullable
@Override
public World getWorld() {
return world.get();
return Optional.ofNullable(world.get()).orElseGet(() -> {
World bukkitWorld = Bukkit.getWorld(worldName);
if (bukkitWorld != null) {
this.world = new WeakReference<>(bukkitWorld);
}
return bukkitWorld;
});
}
@Override

View File

@@ -34,7 +34,7 @@ softdepend:
- GriefDefender
- GriefPrevention
- BentoBox
- IridiumSkyBlock
- IridiumSkyblock
- KingdomsX
- Landlord
- Lands