Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Give items a tag and change them also check if they can be upgraded
- */
- @Override
- public void entityInside(BlockState state, Level level, BlockPos position, Entity entity) {
- if(entity instanceof ItemEntity itemEntity && !itemEntity.getItem().is(Items.AIR)) {
- ItemStack stack = itemEntity.getItem();
- int count = stack.getCount();
- if(stack.hasTag()){
- CompoundTag tag = stack.getOrCreateTag();
- boolean flag1 = tag.contains("blockChangePos");
- if(flag1){
- if(!NbtUtils.readBlockPos((CompoundTag) stack.getOrCreateTag().get("blockChangePos")).equals(position))
- return;
- } else {
- tag.put("blockChangePos", NbtUtils.writeBlockPos(position));
- stack.setTag(tag);
- }
- } else {
- CompoundTag tag = stack.getOrCreateTag();
- tag.put("blockChangePos", NbtUtils.writeBlockPos(position));
- stack.setTag(tag);
- }
- CraftingRecipe[] recipes = getRecipesFor(stack, level);
- int size = recipes.length;
- if(size > 0){
- if(!level.isClientSide){
- CraftingRecipe recipe = recipes[size - 1];
- ItemStack[] recipeItems = getIngredients(recipe, level);
- for(int i = 0; i < count; i++){
- for(ItemStack itemStack : recipeItems){
- ItemEntity item = new ItemEntity(level, position.getX(), position.getY(), position.getZ(), itemStack, 0, 1,0);
- level.addFreshEntity(item);
- entity.discard();
- }
- }
- }
- }
- }
- super.entityInside(state, level, position, entity);
- }
- private static CraftingRecipe[] getRecipesFor(ItemStack inputStack, Level world) {
- List<CraftingRecipe> recipes = new ArrayList<>();
- if (!inputStack.isEmpty()) {
- for (Recipe<?> recipe : world.getRecipeManager().getRecipes()) {
- if (recipe instanceof CraftingRecipe rec && recipe.canCraftInDimensions(3, 3) && !recipe.getIngredients().isEmpty() && matches(inputStack, recipe.getResultItem())) {
- recipes.add(rec);
- }
- }
- }
- return recipes.toArray(new CraftingRecipe[0]);
- }
- private ItemStack[] getIngredients(CraftingRecipe recipe, Level level) {
- ItemStack[] stacks = new ItemStack[recipe.getIngredients().size()];
- for (int i = 0; i < recipe.getIngredients().size(); i++) {
- ItemStack[] matchingStacks = Arrays.stream(recipe.getIngredients().get(i).getItems()).toArray(ItemStack[]::new);
- if(matchingStacks.length > 0){
- int randomNr = level.random.nextInt(matchingStacks.length);
- stacks[i] = matchingStacks[randomNr];
- } else {
- stacks[i] = ItemStack.EMPTY;
- }
- }
- return stacks;
- }
- private static boolean matches(ItemStack input, ItemStack output) {
- return input.is(output.getItem()) && input.getCount() >= output.getCount();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement