mirror of
https://github.com/Ellpeck/ActuallyAdditions.git
synced 2024-11-22 23:28:35 +01:00
Adding my util classes
This commit is contained in:
parent
00418d267f
commit
ebaae73dad
4 changed files with 136 additions and 0 deletions
|
@ -0,0 +1,33 @@
|
||||||
|
package de.ellpeck.actuallyadditions.mod.util;
|
||||||
|
|
||||||
|
import net.minecraft.core.BlockPos;
|
||||||
|
import net.minecraft.core.Direction;
|
||||||
|
import net.minecraft.world.item.ItemStack;
|
||||||
|
import net.minecraft.world.level.Level;
|
||||||
|
import net.minecraft.world.level.block.entity.BlockEntity;
|
||||||
|
import net.minecraft.world.level.block.state.BlockState;
|
||||||
|
import net.neoforged.neoforge.capabilities.Capabilities;
|
||||||
|
import net.neoforged.neoforge.items.IItemHandler;
|
||||||
|
|
||||||
|
import javax.annotation.Nonnull;
|
||||||
|
import javax.annotation.Nullable;
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
|
public class CapHelper {
|
||||||
|
@Nonnull
|
||||||
|
public static Optional<IItemHandler> getItemHandler(@Nonnull Level level, @Nonnull BlockPos pos, @Nullable Direction side) {
|
||||||
|
BlockState blockState = level.getBlockState(pos);
|
||||||
|
if (blockState.hasBlockEntity()) {
|
||||||
|
BlockEntity blockEntity = level.getBlockEntity(pos);
|
||||||
|
if (blockEntity != null) {
|
||||||
|
return Optional.ofNullable(level.getCapability(Capabilities.ItemHandler.BLOCK, pos, level.getBlockState(pos), blockEntity, side));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return Optional.empty();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Nonnull
|
||||||
|
public static Optional<IItemHandler> getItemHandler(ItemStack stack) {
|
||||||
|
return Optional.ofNullable(stack.getCapability(Capabilities.ItemHandler.ITEM));
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,29 @@
|
||||||
|
package de.ellpeck.actuallyadditions.mod.util;
|
||||||
|
|
||||||
|
import net.minecraft.advancements.Advancement;
|
||||||
|
import net.minecraft.advancements.AdvancementHolder;
|
||||||
|
import net.minecraft.data.recipes.RecipeOutput;
|
||||||
|
import net.minecraft.resources.ResourceLocation;
|
||||||
|
import net.minecraft.world.item.crafting.Recipe;
|
||||||
|
import net.neoforged.neoforge.common.conditions.ICondition;
|
||||||
|
|
||||||
|
import javax.annotation.Nonnull;
|
||||||
|
import javax.annotation.Nullable;
|
||||||
|
|
||||||
|
public class NoAdvRecipeOutput implements RecipeOutput {
|
||||||
|
private final RecipeOutput inner;
|
||||||
|
public NoAdvRecipeOutput(RecipeOutput output) {
|
||||||
|
inner = output;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Nonnull
|
||||||
|
@Override
|
||||||
|
public Advancement.Builder advancement() {
|
||||||
|
return inner.advancement();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void accept(@Nonnull ResourceLocation resourceLocation, @Nonnull Recipe<?> recipe, @Nullable AdvancementHolder advancementHolder, @Nonnull ICondition... iConditions) {
|
||||||
|
inner.accept(resourceLocation, recipe, null, iConditions);
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,32 @@
|
||||||
|
package de.ellpeck.actuallyadditions.mod.util;
|
||||||
|
|
||||||
|
import net.minecraft.advancements.Advancement;
|
||||||
|
import net.minecraft.advancements.AdvancementHolder;
|
||||||
|
import net.minecraft.data.recipes.RecipeOutput;
|
||||||
|
import net.minecraft.resources.ResourceLocation;
|
||||||
|
import net.minecraft.world.item.crafting.Recipe;
|
||||||
|
import net.neoforged.neoforge.common.conditions.ICondition;
|
||||||
|
|
||||||
|
import javax.annotation.Nonnull;
|
||||||
|
import javax.annotation.Nullable;
|
||||||
|
import java.util.function.Function;
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
|
public class RecipeInjector<T extends Recipe<?>> implements RecipeOutput {
|
||||||
|
private final RecipeOutput inner;
|
||||||
|
private final Function<T, ? extends T> constructor;
|
||||||
|
public RecipeInjector(RecipeOutput output, Function<T, ? extends T> constructorIn) {
|
||||||
|
inner = output;
|
||||||
|
this.constructor = constructorIn;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Nonnull
|
||||||
|
@Override
|
||||||
|
public Advancement.Builder advancement() {
|
||||||
|
return inner.advancement();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void accept(@Nonnull ResourceLocation resourceLocation, @Nonnull Recipe<?> recipe, @Nullable AdvancementHolder advancementHolder, @Nonnull ICondition... iConditions) {
|
||||||
|
inner.accept(resourceLocation, constructor.apply((T) recipe), advancementHolder, iConditions);
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,42 @@
|
||||||
|
package de.ellpeck.actuallyadditions.mod.util;
|
||||||
|
|
||||||
|
import net.minecraft.nbt.CompoundTag;
|
||||||
|
import net.minecraft.server.MinecraftServer;
|
||||||
|
import net.minecraft.server.level.ServerPlayer;
|
||||||
|
import net.minecraft.world.entity.player.Player;
|
||||||
|
import net.neoforged.bus.api.IEventBus;
|
||||||
|
import net.neoforged.neoforge.event.entity.player.PlayerEvent;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
|
||||||
|
public class RecipeUnlocker {
|
||||||
|
private static String modtag;
|
||||||
|
private static int version;
|
||||||
|
private static String MODID;
|
||||||
|
|
||||||
|
public static void register(String modid, IEventBus bus, int recipeversion) {
|
||||||
|
modtag = modid + "_unlocked";
|
||||||
|
version = recipeversion;
|
||||||
|
MODID = modid;
|
||||||
|
bus.addListener(RecipeUnlocker::onPlayerLoggedIn);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void onPlayerLoggedIn(PlayerEvent.PlayerLoggedInEvent event) {
|
||||||
|
Player player = event.getEntity();
|
||||||
|
|
||||||
|
CompoundTag tag = player.getPersistentData();
|
||||||
|
if (tag.contains(modtag) && tag.getInt(modtag) >= version) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (player instanceof ServerPlayer) {
|
||||||
|
MinecraftServer server = player.getServer();
|
||||||
|
if (server != null) {
|
||||||
|
var recipes = new ArrayList<>(server.getRecipeManager().getRecipes());
|
||||||
|
recipes.removeIf((recipe -> !recipe.id().getNamespace().contains(MODID)));
|
||||||
|
player.awardRecipes(recipes);
|
||||||
|
tag.putInt(modtag, version);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue