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

WorldEdit support

This commit is contained in:
XiaoMoMi
2024-09-07 23:54:36 +08:00
parent 6ca6281507
commit d4a40f5b5c
15 changed files with 511 additions and 15 deletions

View File

@@ -412,4 +412,10 @@ public abstract class AbstractCustomEventListener implements Listener {
}
});
}
@EventHandler(ignoreCancelled = true)
public void onBurn(BlockBurnEvent event) {
Block block = event.getBlock();
itemManager.handlePhysicsBreak(block.getLocation(), block.getBlockData().getAsString(), event);
}
}

View File

@@ -91,6 +91,8 @@ public abstract class ConfigManager implements ConfigLoader, Reloadable {
protected HashSet<Material> overriddenCrops = new HashSet<>();
protected boolean worldeditSupport = false;
public ConfigManager(BukkitCustomCropsPlugin plugin) {
this.plugin = plugin;
instance = this;
@@ -188,6 +190,10 @@ public abstract class ConfigManager implements ConfigLoader, Reloadable {
return instance.overriddenCrops;
}
public static boolean worldeditSupport() {
return instance.worldeditSupport;
}
@Override
public YamlDocument loadConfig(String filePath) {
return loadConfig(filePath, '.');

View File

@@ -18,7 +18,10 @@
package net.momirealms.customcrops.api.core.world;
import com.flowpowered.nbt.CompoundMap;
import com.flowpowered.nbt.CompoundTag;
import net.momirealms.customcrops.api.core.block.CustomCropsBlock;
import net.momirealms.customcrops.api.util.TagUtils;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.NotNull;
/**
@@ -44,4 +47,12 @@ public interface CustomCropsBlockState extends DataBlock {
static CustomCropsBlockState create(CustomCropsBlock owner, CompoundMap compoundMap) {
return new CustomCropsBlockStateImpl(owner, compoundMap);
}
@ApiStatus.Internal
static CustomCropsBlockState create(CustomCropsBlock owner, byte[] nbtBytes) {
return new CustomCropsBlockStateImpl(owner, ((CompoundTag) TagUtils.fromBytes(nbtBytes)).getValue());
}
@ApiStatus.Internal
byte[] getNBTDataAsBytes();
}

View File

@@ -18,9 +18,11 @@
package net.momirealms.customcrops.api.core.world;
import com.flowpowered.nbt.CompoundMap;
import com.flowpowered.nbt.CompoundTag;
import com.flowpowered.nbt.Tag;
import net.momirealms.customcrops.api.core.SynchronizedCompoundMap;
import net.momirealms.customcrops.api.core.block.CustomCropsBlock;
import net.momirealms.customcrops.api.util.TagUtils;
import org.jetbrains.annotations.NotNull;
public class CustomCropsBlockStateImpl implements CustomCropsBlockState {
@@ -39,6 +41,11 @@ public class CustomCropsBlockStateImpl implements CustomCropsBlockState {
return owner;
}
@Override
public byte[] getNBTDataAsBytes() {
return TagUtils.toBytes(new CompoundTag("data", compoundMap.originalMap()));
}
@Override
public Tag<?> set(String key, Tag<?> tag) {
return compoundMap.put(key, tag);

View File

@@ -0,0 +1,58 @@
/*
* 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.api.util;
import com.flowpowered.nbt.Tag;
import com.flowpowered.nbt.stream.NBTInputStream;
import com.flowpowered.nbt.stream.NBTOutputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.nio.ByteOrder;
public class TagUtils {
public static Tag<?> fromBytes(byte[] bytes) {
try {
NBTInputStream nbtInputStream = new NBTInputStream(
new ByteArrayInputStream(bytes),
NBTInputStream.NO_COMPRESSION,
ByteOrder.BIG_ENDIAN
);
return nbtInputStream.readTag();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
public static byte[] toBytes(Tag<?> tag) {
try {
ByteArrayOutputStream outByteStream = new ByteArrayOutputStream();
NBTOutputStream outStream = new NBTOutputStream(
outByteStream,
NBTInputStream.NO_COMPRESSION,
ByteOrder.BIG_ENDIAN
);
outStream.writeTag(tag);
return outByteStream.toByteArray();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}