Advertisement
SforzandoCF

WatcherUP

Nov 19th, 2024 (edited)
38
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 10.80 KB | None | 0 0
  1. package com.sforzando.watcherup;
  2.  
  3. public class WatcherUP {
  4.     public WatcherUP () {
  5.         WatcherUPItems.register(NeoForge.EVENT_BUS);
  6.         WatcherUPBlocks.register(NeoForge.EVENT_BUS);
  7.         WatcherUPEntities.register(NeoForge.EVENT_BUS);
  8.         WatcherUPBlockEntities.register(NeoForge.EVENT_BUS);
  9.     }
  10. }
  11.  
  12. public class WatcherUPItems {
  13.     public static final DeferredRegister ITEMS = DeferredRegister.create(BuiltInRegistries.ITEMS, "watcherup");
  14.    
  15.     public static final HashMap<String, Item> ITEMS_MAP = new HashMap<String, Item>();
  16.    
  17.     public static final Item WATCHER_OIL = register(new Item(new Item.Properties().rarity(Rarity.UNCOMMON)), "watcher_oil");
  18.     public static final Item MIRROR_SCRAPS = register(new MirrorScrapsItem(new Item.Properties().rarity(Rarity.RARE)), "mirror_scraps");
  19.    
  20.     private static Item register (Item i, String key) {
  21.         ITEMS_MAP.put(key, i);
  22.         return i;
  23.     }
  24.    
  25.     public static void register (IEventBus bus) {
  26.         for (String key : ITEMS_MAP.keySet())
  27.             ITEMS.register(key, () -> ITEMS_MAP.get(key));
  28.         ITEMS.register(bus);
  29.     }
  30. }
  31.  
  32. public class WatcherUPBlocks {
  33.     public static final DeferredRegister BLOCKS = DeferredRegister.create(BuiltInRegistries.BLOCKS, "watcherup");
  34.    
  35.     public static final HashMap<String, Block> BLOCKS_MAP = new HashMap<String, Block>();
  36.    
  37.     public static final Block WATCHER_BEACON = register(new WatcherBeaconBlock(BlockBehaviour.Properties.of().mapColor(MapColor.BLUE).noOcclusion().sound(SoundType.NETHERITE).strength(50F).pushReaction(PushReaction.BLOCK)));
  38.    
  39.     private static Block register (Block i, String key) {
  40.         BLOCKS_MAP.put(key, i);
  41.         return i;
  42.     }
  43.    
  44.     public static void register (IEventBus bus) {
  45.         for (String key : BLOCKS_MAP.keySet())
  46.             BLOCKS.register(key, () -> BLOCKS_MAP.get(key));
  47.         BLOCKS.register(bus);
  48.     }
  49. }
  50.  
  51. public class WatcherBeaconBlock extends BaseEntityBlock {
  52.     public WatcherBeaconBlock (BlockBehaviour.Properties settings) {
  53.         super (settings);
  54.     }
  55.    
  56.     protected MapCodec<WatcherBeaconBlock> codec () {
  57.         return AbstractBlock.simpleCodec(WatcherBeaconBlock::new);
  58.     }
  59.    
  60.     public WatcherBeaconBlockEntity newBlockEntity (BlockPos pos, BlockState state) {
  61.         return new WatcherBeaconBlockEntity (pos, state);
  62.     }
  63.    
  64.     public BlockEntityTicker<WatcherBeaconBlockEntity> getTicker (Level world, BlockState state, BlockEntityType<WatcherBeaconBlockEntity> type) {
  65.         return (w, p, s, t) -> { t.tick(); };
  66.     }
  67.    
  68.     public BlockState playerWillDestroy (Level world, BlockPos pos, BlockState state, Player player) {
  69.         WatcherControl.setTarget(player, 2, pos, world);
  70.         world.explode(player, WatcherDamagers.BEACON, new ExplosionDamageCalculator(), pos.getCenter(), 1.5F, false, Level.ExplosionInteraction.BLOCK);
  71.         return WatcherBlocks.DESTROYED_WATCHER_BEACON.defaultBlockState();
  72.     }
  73. }
  74.  
  75. public class WatcherBeaconBlockEntity extends BlockEntity {
  76.     private NonNullList<UUID> watchers = NonNullList.<UUID>create();
  77.    
  78.     public WatcherBeaconBlockEntity (BlockPos pos, BlockState state) {
  79.         super (WatcherBlockEntities.BEACON, pos, state);
  80.     }
  81.    
  82.     public void tick () {
  83.         if (this.summonTimer >= 0) this.summonTimer--;
  84.         if (this.summonTimer == 0) this.summon();
  85.         if (this.watchers.isEmpty()) {
  86.             this.refreshWatchers();
  87.             return;
  88.         }
  89.         if (!this.checkWatchers()) this.broadcast();
  90.     }
  91.    
  92.     private void refreshWatchers () {
  93.         if (this.getLevel() == null) return;
  94.         for (AbstractWatcher w : WatcherControl.getActiveWatchers(pos, this.getLevel()))
  95.             this.watchers.add(w.getUUID());
  96.     }
  97.    
  98.     private boolean checkWatchers () {
  99.         if (this.getLevel() == null) return;
  100.         for (UUID u : this.watchers)
  101.             for (AbstractWatcher w : WatcherControl.getActiveWatchers(pos, this.getLevel()))
  102.                 if (u.equals(w.getUUID()))
  103.                     return true;
  104.         return false;
  105.     }
  106.    
  107.     private void broadcast () {
  108.         Player player = this.getLevel().getEntitiesOfClass(Player.class, AABB.encapsulatingFullBlocks(this.getBlockPos().above(5).south(15).east(15), this.getBlockPos().below(5).north(15).west(15)), (e) -> {
  109.             return e instanceof Player && WatcherControl.getControlLevel((Player) e, this.getBlockPos(), this.getLevel()) > 0;
  110.         }).getFirst();
  111.         switch (WatcherControl.getControlLevel(player, this.getBlockPos(), this.getLevel())) {
  112.             case 1:
  113.                 this.summonTimer = 60;
  114.             case 2:
  115.                 this.summonTimer = 100;
  116.             case 3:
  117.                 this.summonTimer = 200;
  118.             case 4:
  119.                 this.summonTimer = 300;
  120.             case 5:
  121.                 this.summonTimer = 450;
  122.             case 6:
  123.                 this.summonTimer = 600;
  124.             case 7:
  125.                 this.summonTimer = 900;
  126.             case 8:
  127.                 this.summonTimer = 1200;
  128.             case 9:
  129.                 this.summonTimer = 1800;
  130.             default:
  131.                 return -1;
  132.         }
  133.     }
  134.    
  135.     private void summon () {
  136.         Player player = this.getLevel().getEntitiesOfClass(Player.class, AABB.encapsulatingFullBlocks(this.getBlockPos().above(5).south(15).east(15), this.getBlockPos().below(5).north(15).west(15)), (e) -> {
  137.             return e instanceof Player && WatcherControl.getControlLevel((Player) e, this.getBlockPos(), this.getLevel()) > 0;
  138.         }).getFirst();
  139.         WatcherControl.summonWatchers(player, this.getBlockPos(), this.getLevel());
  140.     }
  141. }
  142.  
  143. public class WatcherControl {
  144.     private static HashMap<PlayerHolder, Integer> targets = new HashMap<PlayerHolder, Integer>();
  145.    
  146.     public static void setTarget (Player player, int level, BlockPos pos, Level world) {
  147.         targets.put(new PlayerHolder(player, pos, world), Integer.valueOf(level));
  148.     }
  149.    
  150.     public static int getControlLevel (Player player, BlockPos pos, Level world) {
  151.         Integer val = targets.get(new PlayerHolder(player, pos, level));
  152.         if (val == null) return 0;
  153.         else return val.intValue();
  154.     }
  155.    
  156.     public static List<AbstractWatcher> getActiveWatchers (BlockPos pos, Level world) {
  157.         return world.getEntitiesOfClass(AbstractWatcher.class, AABB.encapsulatingFullBlocks(pos.above(5).south(15).east(15), pos.below(5).north(15).west(15)), (e) -> { return e.isActive(); });
  158.     }
  159.    
  160.     public static void summonWatchers (Player target, BlockPos pos, Level world) {
  161.         WatcherLevels level = WatcherLevels.get(getControlLevel(target, pos, world));
  162.         for (int i = 0; i <= level.drones(); i++) doSpawn(summonWatcher(pos, world, WatcherDrone.class, WatcherEntities.DRONE), world);
  163.         for (int i = 0; i <= level.combatDrones(); i++) summonWatcher(pos, world, WatcherCombatDrone.class, WatcherEntities.COMBAT_DRONE);
  164.         for (int i = 0; i <= level.repairDrones(); i++) summonWatcher(pos, world, WatcherRepairDrone.class, WatcherEntities.REPAIR_DRONE);
  165.         for (int i = 0; i <= level.heavyCombatDrones(); i++) summonWatcher(pos, world, WatcherHeavyCombatDrone.class, WatcherEntities.HEAVY_COMBAT_DRONE);
  166.         for (int i = 0; i <= level.crawlers(); i++) summonWatcher(pos, world, WatcherCrawler.class, WatcherEntities.CRAWLER);
  167.         for (int i = 0; i <= level.mechs(); i++) summonWatcher(pos, world, WatcherMech.class, WatcherEntities.MECH);
  168.         for (int i = 0; i <= level.titans(); i++) summonWatcher(pos, world, WatcherTitan.class, WatcherEntities.TITAN);
  169.     }
  170.    
  171.     public static <T extends AbstractWatcher> T summonWatcher (BlockPos pos, Level world, Class<T> clazz, EntityType<T> type) {
  172.         while (true) {
  173.             BlockPos summonPos;
  174.             int x = world.random.nextIntBetweenInclusive(-15, 15);
  175.             if (x == 0) {
  176.                 summonPos = pos;
  177.             } else is (x < 0) {
  178.                 summonPos = pos.west(-x);
  179.             } else {
  180.                 summonPos = pos.east(x);
  181.             }
  182.             int y = world.random.nextIntBetweenInclusive(-5, 5);
  183.             if (y == 0) {
  184.                 summonPos = pos;
  185.             } else is (y < 0) {
  186.                 summonPos = pos.below(-y);
  187.             } else {
  188.                 summonPos = pos.above(y);
  189.             }
  190.             int z = world.random.nextIntBetweenInclusive(-5, 5);
  191.             if (z == 0) {
  192.                 summonPos = pos;
  193.             } else is (z < 0) {
  194.                 summonPos = pos.north(-z);
  195.             } else {
  196.                 summonPos = pos.south(z);
  197.             }
  198.             if (world.getBlockState(summonPos).getCollisionShape(world, summonPos).isEmpty()) {
  199.                 try {
  200.                     return clazz.getConstructor(EntityType.class, Level.class).newInstance(type, world);
  201.                 } catch (NoSuchMethodException nsme) {
  202.                     throw new IllegalArgumentException("Watcher with no valid constructor passed to WatcherControl to summon", nsme);
  203.                 } catch (SecurityException se) {
  204.                     return null;
  205.                 } catch (IllegalAccessException iae) {
  206.                     throw new IllegalArgumentException("Access restricted to Watcher instantiation", iae);
  207.                 } catch (IllegalArgumentException iae2) {
  208.                     throw new IllegalStateException("Constructor with declared types does not use those types", iae2);
  209.                 } catch (InstantiationException ie) {
  210.                     throw new IllegalArgumentException("Watcher class supplied to summon is abstract and therefore uninstantiable", ie);
  211.                 } catch (InvocationTargetException ite) {
  212.                     if (ite.getCause() != null) {
  213.                         if (ite.getCause() instanceof RuntimeException) throw (RuntimeException) ite.getCause();
  214.                         if (ite.getCause() instanceof Error) throw (Error) ite.getCause();
  215.                         throw new RuntimeException("Exception thrown during Watcher instantiation", ite.getCause());
  216.                     } else {
  217.                         return null;
  218.                     }
  219.                 }
  220.             }
  221.         }
  222.     }
  223.    
  224.     public static void doSpawn (AbstractWatcher watcher, Level world) {
  225.         ServerLevelAccessor accessor;
  226.         if (world instanceof ServerLevel sw) {
  227.             accessor = sw;
  228.         } else {
  229.             accessor = world.getServer().getLevel(world.dimension());
  230.         }
  231.         watcher.finalizeSpawn(accessor, world.getCurrentDifficultyAt(watcher.getBlockPos()), EntitySpawnReason.TRIGGERED, null);
  232.     }
  233. }
  234.  
  235. public class AbstractWatcher extends PathfinderMob {
  236.     public abstract boolean isActive ();
  237.    
  238.     public boolean canBeLeashed () {
  239.         return false;
  240.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement