Advertisement
SforzandoCF

leg lib

Dec 3rd, 2024 (edited)
42
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 11.87 KB | None | 0 0
  1. package com.sforzando.legatolibrary.config;
  2.  
  3. public class ConfigHandler {
  4.     public static void readConfig (Path path, ConfigProfile profile) {
  5.         File file = new File(path.toFile(), profile.getModID() + ".properties");
  6.         profile.logger.info("Reading mod config state from file at: " + file.toString());
  7.         if (!file.exists()) {
  8.             profile.logger.error("Could not find file: " + file.toString());
  9.             return;
  10.         }
  11.         try {
  12.             Properties config = new Properties().load(new FileReader(file));
  13.             for (String key : profile.keySet()) {
  14.                 if (config.getProperty(key) != null) {
  15.                     ConfigValues.setConfigValue(key, config.get(key));
  16.                 } else {
  17.                     profile.logger.error("Could not find key: " + key);
  18.                 }
  19.             }
  20.         } catch (IOFoundException var4) {
  21.             profile.logger.error(var4);
  22.         }
  23.     }
  24.    
  25.     public static void writeConfig (Path path, ConfigProfile profile) {
  26.         File file = new File(path.toFile(), profile.getModID() + ".properties");
  27.         profile.logger.info("Writing mod config state to file at: " + file.toString());
  28.         if (file.exists()) {
  29.             try {
  30.                 file.delete();
  31.                 file.createNewFile();
  32.                 profile.logger.info("Cleared file at: " + file.toString());
  33.             } catch (IOException ioe) {
  34.                 profile.logger.error("Could not clear file at: " + file.toString(), ioe);
  35.                 return;
  36.             }
  37.         } else {
  38.             try {
  39.                 file.createNewFile();
  40.                 profile.logger.info("Created file at: " + file.toString());
  41.             } catch (IOException ioe) {
  42.                 profile.logger.error("Could not create file at: " + file.toString(), ioe);
  43.                 return;
  44.             }
  45.         }
  46.         try {
  47.             Properties config = new Properties();
  48.             for (String key : profile.keySet())
  49.                 config.setProperty(key, profile.get(key));
  50.             config.store(new FileWriter(file), "Config settings for mod with ID: " + profile.getModID());
  51.         } catch (IOFoundException var4) {
  52.             profile.logger.error(var4);
  53.         }
  54.     }
  55. }
  56.  
  57. public class ConfigProfile {
  58.     public final Logger logger;
  59.    
  60.     private final String modID;
  61.     private HashMap<String, String> properties = new HashMap<String, String>();
  62.    
  63.     public ConfigProfile (String id, Logger log) {
  64.         this.modID = id;
  65.         this.logger = log;
  66.     }
  67.    
  68.     public String getModID () {
  69.         return this.modID();
  70.     }
  71.    
  72.     public Set<String> keySet () {
  73.         return this.properties.keySet();
  74.     }
  75.    
  76.     public String get (String key) {
  77.         return this.properties.get(key);
  78.     }
  79. }
  80.  
  81. package com.sforzando.legatolibrary.items;
  82.  
  83. public class ManagedInteractionItem extends Item {
  84.     private final Interaction interaction;
  85.     private final BlockInteraction bInteraction;
  86.    
  87.     public ManagedInteractionItem (Interaction i, BlockInteraction bi, Item.Settings settings) {
  88.         super (settings);
  89.         this.interaction = i;
  90.         this.bInteraction = bi;
  91.     }
  92.    
  93.     public ActionResult use (World world, PlayerEntity player, Hand hand) {
  94.         return this.interaction.use(this, world, player, hand);
  95.     }
  96.    
  97.     public ActionResult useOn (UseOnContext ctx) {
  98.         return this.bInteraction.useOn(this, ctx);
  99.     }
  100.    
  101.     public static interface Interaction {
  102.         ActionResult use (Item item, World world, PlayerEntity player, Hand hand);
  103.        
  104.         static Interaction defaultInteraction () {
  105.             return (i, w, p, h) -> {
  106.                 FoodComponent food = (FoodComponent)p.getItemInHand(h).get(DataComponents.FOOD);
  107.                 if (food != null) {
  108.                     if (p.canEat(food.canAlwaysEat())) {
  109.                         p.startUsingItem(h);
  110.                         return ActionResult.CONSUME;
  111.                     } else {
  112.                         return ActionResultHolder.FAIL;
  113.                     }
  114.                 } else {
  115.                     return ActionResult.PASS;
  116.                 }
  117.             };
  118.         }
  119.     }
  120.    
  121.     public static interface BlockInteraction {
  122.         ActionResult useOn (Item item, UseOnContext ctx);
  123.        
  124.         static BlockInteraction defaultInteraction () {
  125.             return (i, c) -> { return ActionResult.PASS; };
  126.         }
  127.     }
  128. }
  129.  
  130. public class EffectApplierSwordItem extends SwordItem {
  131.     private final StatusEffectInstance effect;
  132.    
  133.     public EffectApplierSwordItem (StatusEffectInstance instance, ToolMaterial material, float damage, float speed, Item.Settings settings) {
  134.         super (material, damage, speed, settings);
  135.         this.effect = instance;
  136.     }
  137.    
  138.     public boolean postHit (ItemStack item, LivingEntity target, LivingEntity attacker) {
  139.         if (!super.postHit(item, target, attacker)) return false;
  140.         target.addStatusEffect(this.effect, attacker);
  141.         return true;
  142.     }
  143. }
  144.  
  145. public class EffectApplierPickaxeItem extends PickaxeItem {
  146.     private final StatusEffectInstance effect;
  147.    
  148.     public EffectApplierPickaxeItem (StatusEffectInstance instance, ToolMaterial material, float damage, float speed, Item.Settings settings) {
  149.         super (material, damage, speed, settings);
  150.         this.effect = instance;
  151.     }
  152.    
  153.     public boolean postHit (ItemStack item, LivingEntity target, LivingEntity attacker) {
  154.         if (!super.postHit(item, target, attacker)) return false;
  155.         target.addStatusEffect(this.effect, attacker);
  156.         return true;
  157.     }
  158. }
  159.  
  160. public class EffectApplierAxeItem extends AxeItem {
  161.     private final StatusEffectInstance effect;
  162.    
  163.     public EffectApplierAxeItem (StatusEffectInstance instance, ToolMaterial material, float damage, float speed, Item.Settings settings) {
  164.         super (material, damage, speed, settings);
  165.         this.effect = instance;
  166.     }
  167.    
  168.     public boolean postHit (ItemStack item, LivingEntity target, LivingEntity attacker) {
  169.         if (!super.postHit(item, target, attacker)) return false;
  170.         target.addStatusEffect(this.effect, attacker);
  171.         return true;
  172.     }
  173. }
  174.  
  175. public class EffectApplierShovelItem extends ShovelItem {
  176.     private final StatusEffectInstance effect;
  177.    
  178.     public EffectApplierShovelItem (StatusEffectInstance instance, ToolMaterial material, float damage, float speed, Item.Settings settings) {
  179.         super (material, damage, speed, settings);
  180.         this.effect = instance;
  181.     }
  182.    
  183.     public boolean postHit (ItemStack item, LivingEntity target, LivingEntity attacker) {
  184.         if (!super.postHit(item, target, attacker)) return false;
  185.         target.addStatusEffect(this.effect, attacker);
  186.         return true;
  187.     }
  188. }
  189.  
  190. public class EffectApplierHoeItem extends HoeItem {
  191.     private final StatusEffectInstance effect;
  192.    
  193.     public EffectApplierPickaxeItem (StatusEffectInstance instance, ToolMaterial material, float damage, float speed, Item.Settings settings) {
  194.         super (material, damage, speed, settings);
  195.         this.effect = instance;
  196.     }
  197.    
  198.     public boolean postHit (ItemStack item, LivingEntity target, LivingEntity attacker) {
  199.         if (!super.postHit(item, target, attacker)) return false;
  200.         target.addStatusEffect(this.effect, attacker);
  201.         return true;
  202.     }
  203. }
  204.  
  205. package com.sforzando.legatolibrary.blocks;
  206.  
  207. public class ManagedInteractionBlock extends Block {
  208.     private final Interaction interaction;
  209.     private final ItemInteraction iInteraction;
  210.    
  211.     public ManagedInteractionBlock (Interaction i, ItemInteraction ii, AbstractBlock.Settings settings) {
  212.         super (settings);
  213.         this.interaction = i;
  214.         this.iInteraction = ii;
  215.     }
  216.    
  217.     public ActionResult onUse (BlockState state, World world, BlockPos pos, PlayerEntity player, BlockHitResult hit) {
  218.         return this.interaction.onUse(this, state, world, pos, player, hit);
  219.     }
  220.    
  221.     public ActionResult onUseWithItem (ItemStack stack, BlockState state, World world, BlockPos pos, PlayerEntity player, Hand hand, BlockHitResult hit) {
  222.         return this.interaction.onUseWithItem(this, stack, state, world, pos, player, hand, hit);
  223.     }
  224.    
  225.     public static interface Interaction {
  226.         ActionResult onUse (Block block, BlockState state, World world, BlockPos pos, PlayerEntity player, BlockHitResult hit);
  227.        
  228.         static Interaction defaultInteraction () {
  229.             return (b, s, w, p, e, h) -> { return InteractionResult.PASS; };
  230.         }
  231.     }
  232.    
  233.     public static interface ItemInteraction {
  234.         ActionResult onUseWithItem (Block block, ItemStack stack, BlockState state, World world, BlockPos pos, PlayerEntity player, Hand hand, BlockHitResult hit);
  235.        
  236.         static ItemInteraction defaultInteraction () {
  237.             return (b, s, w, p, e, h) -> { return ActionResult.PASS_TO_DEFAULT_BLOCK_ACTION; };
  238.         }
  239.     }
  240. }
  241.  
  242. public class ShapedBlock extends Block {
  243.     private final VoxelShape shape;
  244.     private final boolean fullCube;
  245.    
  246.     public ShapedBlock (VoxelShape vs, boolean cube, AbstractBlock.Settings settings) {
  247.         super (settings);
  248.         this.shape = vs;
  249.         this.fullCube = cube;
  250.     }
  251.    
  252.     protected VoxelShape getRaycastShape (BlockState state, BlockView world, BlockPos pos) {
  253.         return this.shape;
  254.     }
  255.    
  256.     protected VoxelShape getOutlineShape (BlockState state, BlockView world, BlockPos pos, ShapeContext context) {
  257.         return this.shape;
  258.     }
  259.    
  260.     protected VoxelShape getCollisionShape (BlockState state, BlockView world, BlockPos pos, ShapeContext context) {
  261.         return this.shape;
  262.     }
  263.    
  264.     protected boolean isShapeFullCube (BlockState state, BlockView world, BlockPos pos) {
  265.         return this.fullCube;
  266.     }
  267.    
  268.     protected VoxelShape getCameraCollisionShape (BlockState state, BlockView world, BlockPos pos, ShapeContext context) {
  269.         return this.shape;
  270.     }
  271. }
  272.  
  273. public class ManagedInteractionAndShapeBlock extends Block {
  274.     private final VoxelShape shape;
  275.     private final boolean fullCube;
  276.    
  277.     private final ManagedInteractionBlock.Interaction interaction;
  278.     private final ManagedInteractionBlock.ItemInteraction iInteraction;
  279.    
  280.     public ManagedInteractionAndShapeBlock (VoxelShape vs, boolean cube, ManagedInteractionBlock.Interaction i, ManagedInteractionBlock.ItemInteraction ii, AbstractBlock.Settings settings) {
  281.         super (settings);
  282.        
  283.         this.shape = vs;
  284.         this.fullCube = cube;
  285.        
  286.         this.interaction = i;
  287.         this.iInteraction = ii;
  288.     }
  289.    
  290.     public InteractionResult onUse (BlockState state, World world, BlockPos pos, PlayerEntity player, BlockHitResult hit) {
  291.         return this.interaction.onUse(this, state, world, pos, player, hit);
  292.     }
  293.    
  294.     public InteractionResult onUseWithItem (ItemStack stack, BlockState state, World world, BlockPos pos, PlayerEntity player, Hand hand, BlockHitResult hit) {
  295.         return this.iInteraction.onUseWithItem(this, stack, state, world, pos, player, hand, hit);
  296.     }
  297.    
  298.     protected VoxelShape getRaycastShape (BlockState state, BlockView world, BlockPos pos) {
  299.         return this.shape;
  300.     }
  301.    
  302.     protected VoxelShape getOutlineShape (BlockState state, BlockView world, BlockPos pos, ShapeContext context) {
  303.         return this.shape;
  304.     }
  305.    
  306.     protected VoxelShape getCollisionShape (BlockState state, BlockView world, BlockPos pos, ShapeContext context) {
  307.         return this.shape;
  308.     }
  309.    
  310.     protected boolean isShapeFullCube (BlockState state, BlockView world, BlockPos pos) {
  311.         return this.fullCube;
  312.     }
  313.    
  314.     protected VoxelShape getCameraCollisionShape (BlockState state, BlockView world, BlockPos pos, ShapeContext context) {
  315.         return this.shape;
  316.     }
  317. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement