Advertisement
SforzandoCF

bttc

Sep 26th, 2024 (edited)
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 9.25 KB | None | 0 0
  1. package com.sforzando.bttc.arch;
  2.  
  3. public enum RotationType {
  4.     NONE(false, false),
  5.     CLOCKWISE(true, false),
  6.     COUNTERCLOCKWISE(true, true);
  7.    
  8.     private final boolean isRotating;
  9.     private final boolean isCounter;
  10.    
  11.     private RotationType (boolean b1, boolean b2) {
  12.         this.isRotating = b1;
  13.         this.isCounter = b2;
  14.     }
  15.    
  16.     public static RotationType getRotationAt (World world, BlockPos pos, Direction dir) {
  17.         if (!(world.getBlockState(pos.offset(1L, dir)).getBlock() instanceof MechanicalPowerTransferBlock)) return RotationType.NONE;
  18.         MechanicalPowerTransferBlock mpt = (MechanicalPowerTransferBlock)world.getBlockState(pos.offset(1L, dir)).getBlock();
  19.         return mpt.getRotationType(world, pos.offset(1L, dir), dir.getOpposite());
  20.     }
  21. }
  22.  
  23. package com.sforzando.bttc.block;
  24.  
  25. public interface MechanicalPowerTransferBlock {
  26.     public RotationType getRotation (World world, BlockPos pos, Direction toward);
  27. }
  28.  
  29. public class AxleBlock extends FacingBlock implements MechanicalPowerTransferBlock {
  30.     public AxleBlock (AbstractBlock.Settings settings) {
  31.         super (settings);
  32.         this.setDefaultState(this.getStateManager().getDefaultState().with(FacingBlock.FACING, Direction.UP));
  33.     }
  34.    
  35.     public BlockState getPlacementState (ItemPlacementContext ctx) {
  36.         return this.getStateManager().getDefaultState().with(FacingBlock.FACING, ctx.getSide());
  37.     }
  38.    
  39.     public RotationType getRotation (World world, BlockPos pos, Direction toward) {
  40.         return ((world.getBlockState(pos).hasProperty(FacingBlock.FACING) && toward.getAxis() == world.getBlockState(pos).getValue(FacingBlock.FACING).getAxis()) || this.shouldSupplyPower(world, pos)) ? RotationType.getRotationAt(world, pos, toward.getOpposite()) : RotationType.NONE;
  41.     }
  42.    
  43.     public boolean shouldSupplyPower (World world, BlockPos pos) {
  44.         if (world.getEntitiesOfClass(WindmillEntity.class, Box.of(pos)).size() > 0) return true;
  45.         if (world.getEntitiesOfClass(VerticalWindmillEntity.class, Box.of(pos)).size() > 0) return true;
  46.         if (world.getEntitiesOfClass(WaterWheelEntity.class, Box.of(pos)).size() > 0) return true;
  47.         return false;
  48.     }
  49. }
  50.  
  51. public class GearboxBlock extends FacingBlock implements MechanicalPowerTransferBlock {
  52.     public GearboxBlock (AbstractBlock.Settings settings) {
  53.         super (settings);
  54.         this.setDefaultState(this.getStateManager().getDefaultState().with(FacingBlock.FACING, Direction.UP));
  55.     }
  56.    
  57.     public BlockState getPlacementState (ItemPlacementContext ctx) {
  58.         return this.getStateManager().getDefaultState().with(FacingBlock.FACING, ctx.getNearestFacingDirection());
  59.     }
  60.    
  61.     public RotationType getRotation (World world, BlockPos pos, Direction toward) {
  62.         return (world.getBlockState(pos).hasProperty(FacingBlock.FACING) && toward != world.getBlockState(pos).getValue(FacingBlock.FACING)) ? RotationType.getRotationAt(world, pos, world.getBlockState(pos).getValue(FacingBlock.FACING)) : RotationType.NONE;
  63.     }
  64. }
  65.  
  66. public class ClutchBlock extends FacingBlock implements MechanicalPowerTransferBlock {
  67.     public ClutchBlock (AbstractBlock.Settings settings) {
  68.         super (settings);
  69.         this.setDefaultState(this.getStateManager().getDefaultState().with(FacingBlock.FACING, Direction.UP));
  70.     }
  71.    
  72.     public BlockState getPlacementState (ItemPlacementContext ctx) {
  73.         return this.getStateManager().getDefaultState().with(FacingBlock.FACING, ctx.getNearestFacingDirection());
  74.     }
  75.    
  76.     public RotationType getRotation (World world, BlockPos pos, Direction toward) {
  77.         return (world.getBlockState(pos).hasProperty(FacingBlock.FACING) && toward != world.getBlockState(pos).getValue(FacingBlock.FACING) && !world.isReceivingRedstonePower(pos)) ? RotationType.getRotationAt(world, pos, world.getBlockState(pos).getValue(FacingBlock.FACING)) : RotationType.NONE;
  78.     }
  79. }
  80.  
  81. public class SawBlock extends FacingBlock implements MechanicalPowerTransferBlock {
  82.     public static RecipeType<SawBlock.Recipe<SawBlock.Recipe.Input<Block>>>> RECIPE_TYPE = getRecipeType();
  83.    
  84.     public SawBlock (AbstractBlock.Settings settings) {
  85.         super (settings);
  86.         this.setDefaultState(this.getStateManager().getDefaultState().with(FacingBlock.FACING, Direction.UP));
  87.     }
  88.    
  89.     public BlockState getPlacementState (ItemPlacementContext ctx) {
  90.         return this.getStateManager().getDefaultState().with(FacingBlock.FACING, ctx.getNearestFacingDirection());
  91.     }
  92.    
  93.     public RotationType getRotation (World world, BlockPos pos, Direction toward) {
  94.         return (world.getBlockState(pos).hasProperty(FacingBlock.FACING) && toward != world.getBlockState(pos).getValue(FacingBlock.FACING) && !world.isReceivingRedstonePower(pos)) ? RotationType.getRotationAt(world, pos, world.getBlockState(pos).getValue(FacingBlock.FACING)) : RotationType.NONE;
  95.     }
  96.    
  97.     public void neighborUpdate (BlockState state, World world, BlockPos pos, Block sourceBlock, BlockPos sourcePos, boolean notify) {
  98.         super (state, world, pos, sourceBlock, sourcePos, notify);
  99.         if (!state.hasProperty(FacingBlock.FACING) || pos.offset(1L, state.getValue(FacingBlock.FACING)) != sourcePos) return;
  100.         Optional<RecipeHolder<SawRecipe>> sawRecipeHolder = new RecipeManager(new DynamicRegistryManager$ImmutableImpl(Registries.REGISTRIES.stream().asList())).getFirstMatch(RECIPE_TYPE, RECIPE_TYPE.recipeFor(sourceBlock), world);
  101.         if (sawRecipeHolder.isPresent()) {
  102.             world.breakBlock(sourcePos, false);
  103.             for (ItemStack i : sawRecipeHolder.get().value().getRemainingItems(sourceBlock))
  104.                 Block.dropItem(world, sourcePos, stack);
  105.         }
  106.     }
  107.    
  108.     private static SawBlock.Recipe.Type<SawBlock.Recipe<SawBlock.Recipe.Input>>> getRecipeType () {
  109.         RecipeType<SawBlock.Recipe<SawBlock.Recipe.Input>>> recipe = new RecipeType<SawBlock.Recipe<SawBlock.Recipe.Input>>>() {};
  110.         Registry.register(Registries.RECIPE_TYPE, recipe, Identifier.of("bttc", "saw_milling"));
  111.         return recipe;
  112.     }
  113. }
  114.  
  115. public class MechanicalBlockRecipeRegistry implements MutableRegistry<Entry> {
  116.     private final RegistryKey<Registry<Entry>> key;
  117.    
  118.     private List<Identifier> keys = List.<Identifier>of();
  119.     private List<Entry> values = List.<Identifier>of();
  120.    
  121.     private MechanicalBlockRecipeRegistry (ResourceKey<Registry<Entry> key) {
  122.         this.key = key;
  123.         keys.add(Identifier.of("bttc", "default"));
  124.         values.add(Entry.getDefault());
  125.     }
  126.    
  127.     public ResourceKey<Registry<Entry>> getKey () {
  128.         return this.key;
  129.     }
  130.    
  131.     public Identifier getId (Entry value) {
  132.         if (values.contains(value))
  133.             return keys.get(values.indexOf(value));
  134.         else
  135.             return null;
  136.     }
  137.    
  138.     public Optional<RegistryKey<Entry>> getKey (Entry value) {
  139.         if (values.contains(value))
  140.             return Optional.<RegistryKey<Entry>>of(RegistryKey.<Entry>of(this.getKey(), keys.get(values.indexOf(value))));
  141.         else
  142.             return Optional.<RegistryKey<Entry>>empty();
  143.     }
  144.    
  145.     public int getRawId (Entry value) {
  146.         return values.indexOf(value);
  147.     }
  148.    
  149.     public Entry get (RegistryKey<Entry> key) {
  150.         if (RegistryKey.getRegistryRef() != this.getKey()) return null;
  151.         return get(key.getValue());
  152.     }
  153.    
  154.     public Entry get (Identifier id) {
  155.         if (keys.contains(id))
  156.             return values.get(keys.indexOf(id));
  157.         else
  158.             return null;
  159.     }
  160.    
  161.     public Optional<RegistryEntryInfo> getEntryInfo (RegistryKey<Entry> key) {
  162.         if (get(key) != null)
  163.             return Optional.<RegistryEntryInfo>of(new RegistrationInfo(new VersionedIdentifier(key.getLocation().getNamespace(), SharedConstants.getGameVersion().getId(), key.getLocation().getPath()), Lifecycle.experimental()));
  164.         else
  165.             return Optional.<RegistryEntry>empty();
  166.     }
  167.    
  168.     public Lifecycle getLifecycle () {
  169.         return Lifecycle.experimental();
  170.     }
  171.    
  172.     public Optional<RegistryEntry.Reference<Entry>> getDefaultEntry () {
  173.         return Optional.<RegistryEntry.Reference<Entry>>of(RegistryEntry.Reference.<Entry>standAlone(this.getReadOnlyWrapper(), RegistryKey.<Entry>of(this.getKey(), Identifier.of("bttc", "default"))));
  174.     }
  175.    
  176.     public Set<Identifier> getIds () {
  177.         return Set.<Identifier>copyOf(this.keys);
  178.     }
  179.    
  180.     public Set<Map.Entry<RegistryKey<Entry>, Entry>> getEntrySet {
  181.         List<AbstractMap.SimpleImmutableEntry<RegistryKey<Entry>, Entry>> entries = List.<AbstractMap.SimpleImmutableEntry<RegistryKey<Entry>, Entry>>of();
  182.         for (Identifier id : this.keys)
  183.             AbstractMap.SimpleImmutableEntry<RegistryKey<Entry>, Entry> = new AbstractMap.SimpleImmutableEntry<RegistryKey<Entry>, Entry>(RegistryKey.<Entry>of(this.getKey(), id), this.get(id));
  184.         return Set.<AbstractMap.SimpleImmutableEntry<RegistryKey<Entry>, Entry>>copyOf(entries);
  185.     }
  186.    
  187.     public Set<RegistryKey<Entry>> getKeys () {
  188.         Set<RegistryKey<Entry>> getKeys = Set.<RegistryKey<Entry>>of();
  189.         this.getIds().forEach((id) -> { getKeys.add(RegistryKey.<Entry>of(this.getKey(), id)); });
  190.         return getKeys;
  191.     }
  192.    
  193.    
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement