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

330 lines
15 KiB
Java
Raw Normal View History

2018-10-14 14:27:18 +02:00
package de.ellpeck.naturesaura;
2021-12-19 18:24:54 +01:00
import com.mojang.blaze3d.systems.RenderSystem;
2021-12-20 00:49:38 +01:00
import com.mojang.blaze3d.vertex.VertexConsumer;
2021-12-29 22:00:52 +01:00
import com.mojang.datafixers.util.Either;
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;
2021-12-19 18:24:54 +01:00
import net.minecraft.client.Minecraft;
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.ChunkHolder;
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;
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.level.chunk.LevelChunk;
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;
import net.minecraftforge.registries.IForgeRegistryEntry;
2021-12-19 16:35:03 +01:00
import org.apache.commons.lang3.tuple.ImmutableTriple;
import top.theillusivec4.curios.api.CuriosApi;
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;
2021-12-29 22:00:52 +01:00
import java.util.concurrent.CompletableFuture;
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++) {
var chunk = 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) {
2021-12-04 19:17:21 +01:00
if (!ItemStack.isSame(first, second))
return false;
2021-12-04 19:17:21 +01:00
return !nbt || ItemStack.tagMatches(first, second);
}
2021-12-19 18:24:54 +01:00
@OnlyIn(Dist.CLIENT)
public static void renderItemInGui(ItemStack stack, int x, int y, float scale) {
var poseStack = RenderSystem.getModelViewStack();
poseStack.pushPose();
poseStack.translate(x, y, 0);
poseStack.scale(scale, scale, scale);
RenderSystem.applyModelViewMatrix();
Minecraft.getInstance().getItemRenderer().renderGuiItem(stack, 0, 0);
Minecraft.getInstance().getItemRenderer().renderGuiItemDecorations(Minecraft.getInstance().font, stack, 0, 0, null);
poseStack.popPose();
RenderSystem.applyModelViewMatrix();
}
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) {
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()) {
2021-12-04 19:17:21 +01:00
var remain = handler.insertItem(slot, handStack, player.level.isClientSide);
if (!ItemStack.matches(remain, handStack)) {
if (sound)
2021-12-04 15:40:09 +01: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);
2021-12-04 15:40:09 +01: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)
2021-12-04 15:40:09 +01: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);
2021-12-04 15:40:09 +01:00
if (!player.level.isClientSide) {
2021-12-04 19:17:21 +01:00
var stack = handler.getStackInSlot(slot);
if (!player.addItem(stack)) {
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)
2020-01-25 19:18:45 +01:00
return 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()) {
var changed = 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;
2021-12-04 19:17:21 +01:00
var adv = playerMp.getLevel().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...
public static <T extends IForgeRegistryEntry<T>> void populateObjectHolders(Class<?> clazz, IForgeRegistry<T> registry) {
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
2021-12-04 15:40:09 +01:00
public static ItemStack getEquippedItem(Predicate<ItemStack> predicate, Player player) {
2021-12-19 16:35:03 +01:00
if (Compat.hasCompat("curios")) {
var stack = CuriosApi.getCuriosHelper().findEquippedCurio(predicate, player).map(ImmutableTriple::getRight);
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
}
2021-12-04 19:17:21 +01:00
for (var i = 0; i < player.getInventory().getContainerSize(); i++) {
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;
}
2018-10-14 14:27:18 +02:00
}