NaturesAura/src/main/java/de/ellpeck/naturesaura/Helper.java

379 lines
17 KiB
Java
Raw Normal View History

2018-10-14 14:27:18 +02:00
package de.ellpeck.naturesaura;
2021-12-20 00:49:38 +01:00
import com.mojang.blaze3d.vertex.VertexConsumer;
2018-11-12 22:04:40 +01:00
import de.ellpeck.naturesaura.api.NaturesAuraAPI;
2020-01-25 19:18:45 +01:00
import de.ellpeck.naturesaura.api.aura.container.IAuraContainer;
2018-11-11 13:26:19 +01:00
import de.ellpeck.naturesaura.api.aura.item.IAuraRecharge;
2021-12-04 15:40:09 +01:00
import de.ellpeck.naturesaura.api.misc.ILevelData;
import de.ellpeck.naturesaura.blocks.tiles.BlockEntityImpl;
import de.ellpeck.naturesaura.chunk.AuraChunk;
2021-12-19 16:35:03 +01:00
import de.ellpeck.naturesaura.compat.Compat;
2021-12-04 15:40:09 +01:00
import de.ellpeck.naturesaura.misc.LevelData;
2023-02-17 14:21:39 +01:00
import de.ellpeck.naturesaura.packet.PacketHandler;
import de.ellpeck.naturesaura.packet.PacketParticles;
2021-12-19 18:24:54 +01:00
import net.minecraft.client.Minecraft;
2023-07-08 12:32:27 +02:00
import net.minecraft.client.gui.GuiGraphics;
2021-12-04 19:17:21 +01:00
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.resources.ResourceLocation;
2021-12-29 22:00:52 +01:00
import net.minecraft.server.level.ServerChunkCache;
2021-12-04 19:17:21 +01:00
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.sounds.SoundEvents;
import net.minecraft.sounds.SoundSource;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.entity.decoration.ItemFrame;
import net.minecraft.world.entity.item.ItemEntity;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.crafting.Ingredient;
import net.minecraft.world.level.ChunkPos;
import net.minecraft.world.level.Level;
2021-12-15 16:24:53 +01:00
import net.minecraft.world.level.LevelAccessor;
2023-02-17 14:21:39 +01:00
import net.minecraft.world.level.block.Blocks;
2021-12-04 19:17:21 +01:00
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.block.state.properties.Property;
2021-12-29 22:00:52 +01:00
import net.minecraft.world.level.chunk.ChunkAccess;
import net.minecraft.world.level.chunk.ChunkStatus;
2021-12-04 19:17:21 +01:00
import net.minecraft.world.phys.AABB;
import net.minecraft.world.phys.Vec3;
2020-01-21 21:04:44 +01:00
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;
2018-10-20 21:19:08 +02:00
import net.minecraftforge.common.capabilities.Capability;
import net.minecraftforge.common.capabilities.ICapabilityProvider;
2020-01-21 21:04:44 +01:00
import net.minecraftforge.common.util.LazyOptional;
2019-02-17 22:51:05 +01:00
import net.minecraftforge.items.IItemHandler;
2020-01-21 21:04:44 +01:00
import net.minecraftforge.registries.ForgeRegistries;
2020-01-22 01:32:26 +01:00
import net.minecraftforge.registries.IForgeRegistry;
2021-12-19 16:35:03 +01:00
import top.theillusivec4.curios.api.CuriosApi;
2023-02-15 23:45:50 +01:00
import top.theillusivec4.curios.api.SlotResult;
2018-10-14 14:27:18 +02:00
2018-10-20 21:19:08 +02:00
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
2020-01-22 01:32:26 +01:00
import java.lang.reflect.Modifier;
2018-10-14 14:27:18 +02:00
import java.util.List;
2020-01-22 01:32:26 +01:00
import java.util.Locale;
import java.util.function.Consumer;
2018-11-14 19:14:03 +01:00
import java.util.function.Function;
2020-01-26 19:26:50 +01:00
import java.util.function.Predicate;
2018-10-14 14:27:18 +02:00
public final class Helper {
2021-12-15 16:24:53 +01:00
public static boolean getBlockEntitiesInArea(LevelAccessor level, BlockPos pos, int radius, Function<BlockEntity, Boolean> consumer) {
2021-12-04 19:17:21 +01:00
for (var x = pos.getX() - radius >> 4; x <= pos.getX() + radius >> 4; x++) {
for (var z = pos.getZ() - radius >> 4; z <= pos.getZ() + radius >> 4; z++) {
2022-06-27 15:24:04 +02:00
var chunk = Helper.getLoadedChunk(level, x, z);
2020-01-22 16:03:39 +01:00
if (chunk != null) {
2021-12-04 19:17:21 +01:00
for (var tilePos : chunk.getBlockEntitiesPos()) {
if (tilePos.distSqr(pos) <= radius * radius)
2021-12-04 15:40:09 +01:00
if (consumer.apply(chunk.getBlockEntity(tilePos)))
2018-11-14 19:14:03 +01:00
return true;
2018-10-14 14:27:18 +02:00
}
}
}
}
2018-11-14 19:14:03 +01:00
return false;
2018-10-14 14:27:18 +02:00
}
2021-12-04 15:40:09 +01:00
public static void getAuraChunksWithSpotsInArea(Level level, BlockPos pos, int radius, Consumer<AuraChunk> consumer) {
2021-12-04 19:17:21 +01:00
var data = (LevelData) ILevelData.getLevelData(level);
for (var x = pos.getX() - radius >> 4; x <= pos.getX() + radius >> 4; x++) {
for (var z = pos.getZ() - radius >> 4; z <= pos.getZ() + radius >> 4; z++) {
var chunk = data.auraChunksWithSpots.get(ChunkPos.asLong(x, z));
if (chunk != null)
consumer.accept(chunk);
}
}
}
2021-12-04 19:17:21 +01:00
public static List<ItemFrame> getAttachedItemFrames(Level level, BlockPos pos) {
var frames = level.getEntitiesOfClass(ItemFrame.class, new AABB(pos).inflate(0.25));
for (var i = frames.size() - 1; i >= 0; i--) {
var frame = frames.get(i);
var framePos = frame.getPos().relative(frame.getDirection().getOpposite());
2018-11-05 16:36:10 +01:00
if (!pos.equals(framePos))
frames.remove(i);
}
return frames;
}
2021-12-29 22:00:52 +01:00
public static ChunkAccess getLoadedChunk(LevelAccessor level, int x, int z) {
// as always, this code is EXTREMELY FRAGILE and editing it before the next major version will probably break it
2021-12-29 22:00:52 +01:00
if (level.getChunkSource() instanceof ServerChunkCache cache) {
return cache.isPositionTicking(ChunkPos.asLong(x, z)) ? cache.getChunk(x, z, ChunkStatus.FULL, false) : null;
} else {
return level.getChunk(x, z, ChunkStatus.FULL, false);
2021-12-29 22:00:52 +01:00
}
2018-10-30 18:18:56 +01:00
}
2018-10-15 18:36:46 +02:00
public static int blendColors(int c1, int c2, float ratio) {
2021-12-04 19:17:21 +01:00
var a = (int) ((c1 >> 24 & 0xFF) * ratio + (c2 >> 24 & 0xFF) * (1 - ratio));
var r = (int) ((c1 >> 16 & 0xFF) * ratio + (c2 >> 16 & 0xFF) * (1 - ratio));
var g = (int) ((c1 >> 8 & 0xFF) * ratio + (c2 >> 8 & 0xFF) * (1 - ratio));
var b = (int) ((c1 & 0xFF) * ratio + (c2 & 0xFF) * (1 - ratio));
return (a & 255) << 24 | (r & 255) << 16 | (g & 255) << 8 | b & 255;
2018-10-15 18:36:46 +02:00
}
2018-10-16 01:36:30 +02:00
public static boolean areItemsEqual(ItemStack first, ItemStack second, boolean nbt) {
2023-07-08 12:32:27 +02:00
// TODO see if this is the correct new comparison method?
return nbt ? ItemStack.isSameItemSameTags(first, second) : ItemStack.isSameItem(first, second);
}
2021-12-19 18:24:54 +01:00
@OnlyIn(Dist.CLIENT)
2023-07-08 12:32:27 +02:00
public static void renderItemInGui(GuiGraphics graphics, ItemStack stack, int x, int y, float scale) {
var poseStack = graphics.pose();
2021-12-19 18:24:54 +01:00
poseStack.pushPose();
poseStack.translate(x, y, 0);
poseStack.scale(scale, scale, scale);
2023-07-25 14:12:29 +02:00
graphics.setColor(1, 1, 1, 1);
2023-07-08 12:32:27 +02:00
graphics.renderItem(stack, 0, 0);
graphics.renderItemDecorations(Minecraft.getInstance().font, stack, 0, 0, null);
2021-12-19 18:24:54 +01:00
poseStack.popPose();
}
2021-12-20 00:49:38 +01:00
@OnlyIn(Dist.CLIENT)
public static void renderWeirdBox(VertexConsumer buffer, double x, double y, double z, double width, double height, double depth, float r, float g, float b, float a) {
buffer.vertex(x, y + height, z).color(r, g, b, a).endVertex();
buffer.vertex(x + width, y + height, z).color(r, g, b, a).endVertex();
buffer.vertex(x + width, y, z).color(r, g, b, a).endVertex();
buffer.vertex(x, y, z).color(r, g, b, a).endVertex();
buffer.vertex(x + width, y, z + depth).color(r, g, b, a).endVertex();
buffer.vertex(x + width, y, z).color(r, g, b, a).endVertex();
buffer.vertex(x + width, y + height, z).color(r, g, b, a).endVertex();
buffer.vertex(x + width, y + height, z + depth).color(r, g, b, a).endVertex();
buffer.vertex(x + width, y + height, z + depth).color(r, g, b, a).endVertex();
buffer.vertex(x, y + height, z + depth).color(r, g, b, a).endVertex();
buffer.vertex(x, y, z + depth).color(r, g, b, a).endVertex();
buffer.vertex(x + width, y, z + depth).color(r, g, b, a).endVertex();
buffer.vertex(x, y + height, z + depth).color(r, g, b, a).endVertex();
buffer.vertex(x, y + height, z).color(r, g, b, a).endVertex();
buffer.vertex(x, y, z).color(r, g, b, a).endVertex();
buffer.vertex(x, y, z + depth).color(r, g, b, a).endVertex();
buffer.vertex(x, y + height, z).color(r, g, b, a).endVertex();
buffer.vertex(x, y + height, z + depth).color(r, g, b, a).endVertex();
buffer.vertex(x + width, y + height, z + depth).color(r, g, b, a).endVertex();
buffer.vertex(x + width, y + height, z).color(r, g, b, a).endVertex();
buffer.vertex(x + width, y, z).color(r, g, b, a).endVertex();
buffer.vertex(x + width, y, z + depth).color(r, g, b, a).endVertex();
buffer.vertex(x, y, z + depth).color(r, g, b, a).endVertex();
buffer.vertex(x, y, z).color(r, g, b, a).endVertex();
}
2021-12-04 19:17:21 +01:00
public static InteractionResult putStackOnTile(Player player, InteractionHand hand, BlockPos pos, int slot, boolean sound) {
2023-07-08 12:32:27 +02:00
var tile = player.level().getBlockEntity(pos);
2021-12-04 15:40:09 +01:00
if (tile instanceof BlockEntityImpl) {
2021-12-04 19:17:21 +01:00
var handler = ((BlockEntityImpl) tile).getItemHandler();
2018-10-18 13:34:37 +02:00
if (handler != null) {
2021-12-04 19:17:21 +01:00
var handStack = player.getItemInHand(hand);
2018-10-18 13:34:37 +02:00
if (!handStack.isEmpty()) {
2023-07-08 12:32:27 +02:00
var remain = handler.insertItem(slot, handStack, player.level().isClientSide);
if (!ItemStack.matches(remain, handStack)) {
if (sound)
2023-07-08 12:32:27 +02:00
player.level().playSound(player, pos.getX() + 0.5, pos.getY() + 0.5, pos.getZ() + 0.5,
2021-12-04 19:17:21 +01:00
SoundEvents.ITEM_FRAME_ADD_ITEM, SoundSource.PLAYERS, 0.75F, 1F);
2023-07-08 12:32:27 +02:00
if (!player.level().isClientSide)
2021-12-04 19:17:21 +01:00
player.setItemInHand(hand, remain);
2021-12-04 15:40:09 +01:00
return InteractionResult.SUCCESS;
2018-10-18 13:34:37 +02:00
}
}
if (!handler.getStackInSlot(slot).isEmpty()) {
if (sound)
2023-07-08 12:32:27 +02:00
player.level().playSound(player, pos.getX() + 0.5, pos.getY() + 0.5, pos.getZ() + 0.5,
2021-12-04 19:17:21 +01:00
SoundEvents.ITEM_FRAME_REMOVE_ITEM, SoundSource.PLAYERS, 0.75F, 1F);
2023-07-08 12:32:27 +02:00
if (!player.level().isClientSide) {
2021-12-04 19:17:21 +01:00
var stack = handler.getStackInSlot(slot);
if (!player.addItem(stack)) {
2023-07-08 12:32:27 +02:00
var item = new ItemEntity(player.level(), player.getX(), player.getY(), player.getZ(), stack);
player.level().addFreshEntity(item);
}
2018-10-18 13:34:37 +02:00
handler.setStackInSlot(slot, ItemStack.EMPTY);
}
2021-12-04 15:40:09 +01:00
return InteractionResult.SUCCESS;
2018-10-18 13:34:37 +02:00
}
}
}
2021-12-04 15:40:09 +01:00
return InteractionResult.CONSUME;
2018-10-18 13:34:37 +02:00
}
2018-10-20 21:19:08 +02:00
2018-12-01 18:56:05 +01:00
public static ICapabilityProvider makeRechargeProvider(ItemStack stack, boolean needsSelected) {
2018-10-20 21:19:08 +02:00
return new ICapabilityProvider() {
2020-10-19 21:26:32 +02:00
private final LazyOptional<IAuraRecharge> recharge = LazyOptional.of(() -> (container, containerSlot, itemSlot, isSelected) -> {
if (isSelected || !needsSelected)
2022-06-27 15:24:04 +02:00
return Helper.rechargeAuraItem(stack, container, 300);
2018-12-01 18:56:05 +01:00
return false;
2020-10-19 21:26:32 +02:00
});
2018-10-20 21:19:08 +02:00
@Nullable
@Override
2020-01-21 21:04:44 +01:00
public <T> LazyOptional<T> getCapability(@Nonnull Capability<T> capability, @Nullable Direction facing) {
2021-12-23 13:27:52 +01:00
if (capability == NaturesAuraAPI.CAP_AURA_RECHARGE)
2020-10-19 21:26:32 +02:00
return this.recharge.cast();
2020-01-21 21:04:44 +01:00
return LazyOptional.empty();
2018-10-20 21:19:08 +02:00
}
};
}
2020-01-25 19:18:45 +01:00
public static boolean rechargeAuraItem(ItemStack stack, IAuraContainer container, int toDrain) {
2021-12-04 19:17:21 +01:00
if (stack.getDamageValue() > 0 && container.drainAura(toDrain, true) >= toDrain) {
stack.setDamageValue(stack.getDamageValue() - 1);
2020-01-25 19:18:45 +01:00
container.drainAura(toDrain, false);
return true;
}
return false;
}
2019-10-20 22:30:49 +02:00
public static BlockState getStateFromString(String raw) {
2021-12-04 19:17:21 +01:00
var split = raw.split("\\[");
var block = ForgeRegistries.BLOCKS.getValue(new ResourceLocation(split[0]));
if (block != null) {
2021-12-04 19:17:21 +01:00
var state = block.defaultBlockState();
if (split.length > 1) {
2021-12-04 19:17:21 +01:00
for (var part : split[1].replace("]", "").split(",")) {
var keyValue = part.split("=");
for (var prop : state.getProperties()) {
2022-06-27 15:24:04 +02:00
var changed = Helper.findProperty(state, prop, keyValue[0], keyValue[1]);
if (changed != null) {
state = changed;
break;
}
}
}
}
return state;
} else
return null;
}
2020-09-22 03:17:02 +02:00
private static <T extends Comparable<T>> BlockState findProperty(BlockState state, Property<T> prop, String key, String newValue) {
if (key.equals(prop.getName()))
2021-12-04 19:17:21 +01:00
for (var value : prop.getPossibleValues())
if (prop.getName(value).equals(newValue))
2021-12-04 19:17:21 +01:00
return state.setValue(prop, value);
return null;
}
2018-11-12 22:04:40 +01:00
2021-12-04 15:40:09 +01:00
public static void addAdvancement(Player player, ResourceLocation advancement, String criterion) {
2021-12-04 19:17:21 +01:00
if (!(player instanceof ServerPlayer playerMp))
2020-01-23 22:40:03 +01:00
return;
2023-07-08 12:32:27 +02:00
var adv = playerMp.level().getServer().getAdvancements().getAdvancement(advancement);
if (adv != null)
2021-12-04 19:17:21 +01:00
playerMp.getAdvancements().award(adv, criterion);
}
2018-12-30 20:37:00 +01:00
public static int getIngredientAmount(Ingredient ingredient) {
2021-12-04 19:17:21 +01:00
var highestAmount = 0;
for (var stack : ingredient.getItems())
2018-12-30 20:37:00 +01:00
if (stack.getCount() > highestAmount)
highestAmount = stack.getCount();
return highestAmount;
}
2019-01-27 13:57:34 +01:00
2021-12-04 15:40:09 +01:00
public static boolean isHoldingItem(Player player, Item item) {
2021-12-04 19:17:21 +01:00
for (var hand : InteractionHand.values()) {
var stack = player.getItemInHand(hand);
2019-01-27 13:57:34 +01:00
if (!stack.isEmpty() && stack.getItem() == item)
return true;
}
return false;
}
2019-02-17 22:51:05 +01:00
public static boolean isEmpty(IItemHandler handler) {
2021-12-04 19:17:21 +01:00
for (var i = 0; i < handler.getSlots(); i++)
2019-02-17 22:51:05 +01:00
if (!handler.getStackInSlot(i).isEmpty())
return false;
return true;
}
2021-12-04 19:17:21 +01:00
public static AABB aabb(Vec3 pos) {
return new AABB(pos.x, pos.y, pos.z, pos.x, pos.y, pos.z);
}
2020-01-22 16:03:39 +01:00
2021-12-04 19:17:21 +01:00
// This is how @ObjectHolder SHOULD work...
2022-06-27 15:24:04 +02:00
public static <T> void populateObjectHolders(Class<?> clazz, IForgeRegistry<T> registry) {
2021-12-04 19:17:21 +01:00
for (var entry : clazz.getFields()) {
2020-01-22 01:32:26 +01:00
if (!Modifier.isStatic(entry.getModifiers()))
continue;
2021-12-04 19:17:21 +01:00
var location = new ResourceLocation(NaturesAura.MOD_ID, entry.getName().toLowerCase(Locale.ROOT));
2020-01-23 19:20:47 +01:00
if (!registry.containsKey(location)) {
NaturesAura.LOGGER.fatal("Couldn't find entry named " + location + " in registry " + registry.getRegistryName());
continue;
}
2020-01-22 01:32:26 +01:00
try {
2020-01-23 19:20:47 +01:00
entry.set(null, registry.getValue(location));
2020-01-22 01:32:26 +01:00
} catch (IllegalAccessException e) {
NaturesAura.LOGGER.error(e);
}
}
}
2020-01-22 20:31:09 +01:00
public static ItemStack getEquippedItem(Predicate<ItemStack> predicate, Player player, boolean hotbarOnly) {
2021-12-19 16:35:03 +01:00
if (Compat.hasCompat("curios")) {
2023-02-15 23:45:50 +01:00
var stack = CuriosApi.getCuriosHelper().findFirstCurio(player, predicate).map(SlotResult::stack);
2020-01-26 19:26:50 +01:00
if (stack.isPresent())
2020-09-22 16:16:25 +02:00
return stack.get();
2021-12-19 16:35:03 +01:00
}
var invSize = hotbarOnly ? 9 : player.getInventory().getContainerSize();
for (var i = 0; i < invSize; i++) {
2021-12-04 19:17:21 +01:00
var slot = player.getInventory().getItem(i);
2020-01-26 19:26:50 +01:00
if (!slot.isEmpty() && predicate.test(slot))
return slot;
}
return ItemStack.EMPTY;
}
public static BlockPos getClosestAirAboveGround(Level level, BlockPos pos, int radius) {
for (var i = 0; i < radius; i++) {
var up = pos.above(i);
if (level.isEmptyBlock(up) && !level.isEmptyBlock(up.below()))
return up;
var dn = pos.below(i);
if (level.isEmptyBlock(dn) && !level.isEmptyBlock(dn.below()))
return dn;
}
return pos;
}
2023-02-17 14:21:39 +01:00
public static void mineRecursively(Level level, BlockPos pos, BlockPos start, boolean drop, int horizontalRange, int verticalRange, Predicate<BlockState> filter) {
if (Math.abs(pos.getX() - start.getX()) >= horizontalRange || Math.abs(pos.getZ() - start.getZ()) >= horizontalRange || Math.abs(pos.getY() - start.getY()) >= verticalRange)
return;
2023-02-28 14:00:57 +01:00
if (drop) {
level.destroyBlock(pos, true);
} else {
// in this case we don't want the block breaking particles, so we can't use destroyBlock
level.setBlockAndUpdate(pos, Blocks.AIR.defaultBlockState());
PacketHandler.sendToAllAround(level, pos, 32, new PacketParticles(pos.getX(), pos.getY(), pos.getZ(), PacketParticles.Type.TR_DISAPPEAR));
}
2023-02-17 14:21:39 +01:00
for (var x = -1; x <= 1; x++) {
for (var y = -1; y <= 1; y++) {
for (var z = -1; z <= 1; z++) {
2023-02-28 14:00:57 +01:00
if (x == 0 && y == 0 && z == 0)
continue;
2023-02-17 14:21:39 +01:00
var offset = pos.offset(x, y, z);
var state = level.getBlockState(offset);
2023-02-28 14:00:57 +01:00
if (filter.test(state))
2023-02-17 14:21:39 +01:00
Helper.mineRecursively(level, offset, start, drop, horizontalRange, verticalRange, filter);
}
}
}
}
public static boolean isToolEnabled(ItemStack stack) {
return stack.hasTag() && !stack.getTag().getBoolean(NaturesAura.MOD_ID + ":disabled");
}
public static boolean toggleToolEnabled(Player player, ItemStack stack) {
if (!player.isShiftKeyDown())
return false;
var disabled = !Helper.isToolEnabled(stack);
stack.getOrCreateTag().putBoolean(NaturesAura.MOD_ID + ":disabled", !disabled);
2023-07-08 12:32:27 +02:00
player.level().playSound(null, player.getX() + 0.5, player.getY() + 0.5, player.getZ() + 0.5, SoundEvents.ARROW_HIT_PLAYER, SoundSource.PLAYERS, 0.65F, 1F);
return true;
}
2023-07-25 14:12:29 +02:00
2018-10-14 14:27:18 +02:00
}