Refactoring for Scrapbox Dispenser behaviour.

This commit is contained in:
drcrazy 2020-01-10 04:01:53 +03:00
parent 3f2f186b56
commit 8db5cea07c
3 changed files with 43 additions and 67 deletions

View file

@ -26,7 +26,6 @@ package techreborn;
import net.fabricmc.api.ModInitializer;
import net.fabricmc.fabric.api.client.itemgroup.FabricItemGroupBuilder;
import net.minecraft.block.DispenserBlock;
import net.minecraft.item.ItemGroup;
import net.minecraft.item.ItemStack;
import net.minecraft.util.Identifier;
@ -44,7 +43,6 @@ import techreborn.events.ModRegistry;
import techreborn.init.*;
import techreborn.packets.ClientboundPackets;
import techreborn.packets.ServerboundPackets;
import techreborn.utils.BehaviorDispenseScrapbox;
import techreborn.world.WorldGenerator;
public class TechReborn implements ModInitializer {
@ -78,11 +76,7 @@ public class TechReborn implements ModInitializer {
FluidGeneratorRecipes.init();
//Force loads the block entities at the right time
TRBlockEntities.THERMAL_GEN.toString();
// Scrapbox
if (TechRebornConfig.dispenseScrapboxes) {
DispenserBlock.registerBehavior(TRContent.SCRAP_BOX, new BehaviorDispenseScrapbox());
}
TRDispenserBehavior.init();
Torus.genSizeMap(TechRebornConfig.fusionControlComputerMaxCoilSize);

View file

@ -0,0 +1,42 @@
package techreborn.init;
import net.minecraft.block.DispenserBlock;
import net.minecraft.block.dispenser.ItemDispenserBehavior;
import net.minecraft.item.ItemStack;
import net.minecraft.util.math.BlockPointer;
import net.minecraft.util.math.Direction;
import net.minecraft.util.math.Position;
import reborncore.common.crafting.RebornRecipe;
import techreborn.config.TechRebornConfig;
import java.util.List;
import java.util.Random;
/**
* Created by drcrazy on 10-Jan-20 for TechReborn-1.15.
*/
public class TRDispenserBehavior {
public static void init(){
if (TechRebornConfig.dispenseScrapboxes) {
DispenserBlock.registerBehavior(TRContent.SCRAP_BOX, new ItemDispenserBehavior() {
public ItemStack dispenseSilently(BlockPointer pointer, ItemStack stack){
List<RebornRecipe> scrapboxRecipeList = ModRecipes.SCRAPBOX.getRecipes(pointer.getWorld());
int random = new Random().nextInt(scrapboxRecipeList.size());
ItemStack out = scrapboxRecipeList.get(random).getOutputs().get(0);
stack.split(1);
Direction facing = pointer.getBlockState().get(DispenserBlock.FACING);
Position position = DispenserBlock.getOutputLocation(pointer);
spawnItem(pointer.getWorld(), out, 6, facing, position);
return stack;
}
} );
}
// DispenserBlock.registerBehavior(TRContent.CELL, new ItemDispenserBehavior(){
//
// });
}
}

View file

@ -1,60 +0,0 @@
/*
* This file is part of TechReborn, licensed under the MIT License (MIT).
*
* Copyright (c) 2020 TechReborn
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package techreborn.utils;
import net.minecraft.block.DispenserBlock;
import net.minecraft.block.dispenser.ItemDispenserBehavior;
import net.minecraft.block.entity.DispenserBlockEntity;
import net.minecraft.item.ItemStack;
import net.minecraft.util.math.BlockPointer;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Direction;
import net.minecraft.util.math.Position;
import reborncore.common.crafting.RebornRecipe;
import techreborn.config.TechRebornConfig;
import techreborn.init.ModRecipes;
import java.util.List;
import java.util.Random;
public class BehaviorDispenseScrapbox extends ItemDispenserBehavior {
@Override
protected ItemStack dispenseSilently(BlockPointer source, ItemStack stack) {
if (TechRebornConfig.dispenseScrapboxes) {
List<RebornRecipe> scrapboxRecipeList = ModRecipes.SCRAPBOX.getRecipes(source.getWorld());
int random = new Random().nextInt(scrapboxRecipeList.size());
ItemStack out = scrapboxRecipeList.get(random).getOutputs().get(0);
stack.split(1);
DispenserBlockEntity blockEntity = source.getBlockEntity();
Direction enumfacing = blockEntity.getWorld().getBlockState(new BlockPos(source.getX(), source.getY(), source.getZ())).get(DispenserBlock.FACING);
Position iposition = DispenserBlock.getOutputLocation(source);
spawnItem(source.getWorld(), out, 6, enumfacing, iposition);
}
return stack;
}
}