Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.sforzando.watcherup;
- public class WatcherUP {
- public WatcherUP () {
- WatcherUPItems.register(NeoForge.EVENT_BUS);
- WatcherUPBlocks.register(NeoForge.EVENT_BUS);
- WatcherUPEntities.register(NeoForge.EVENT_BUS);
- WatcherUPBlockEntities.register(NeoForge.EVENT_BUS);
- }
- }
- public class WatcherUPItems {
- public static final DeferredRegister ITEMS = DeferredRegister.create(BuiltInRegistries.ITEMS, "watcherup");
- public static final HashMap<String, Item> ITEMS_MAP = new HashMap<String, Item>();
- public static final Item WATCHER_OIL = register(new Item(new Item.Properties().rarity(Rarity.UNCOMMON)), "watcher_oil");
- public static final Item MIRROR_SCRAPS = register(new MirrorScrapsItem(new Item.Properties().rarity(Rarity.RARE)), "mirror_scraps");
- private static Item register (Item i, String key) {
- ITEMS_MAP.put(key, i);
- return i;
- }
- public static void register (IEventBus bus) {
- for (String key : ITEMS_MAP.keySet())
- ITEMS.register(key, () -> ITEMS_MAP.get(key));
- ITEMS.register(bus);
- }
- }
- public class WatcherUPBlocks {
- public static final DeferredRegister BLOCKS = DeferredRegister.create(BuiltInRegistries.BLOCKS, "watcherup");
- public static final HashMap<String, Block> BLOCKS_MAP = new HashMap<String, Block>();
- public static final Block WATCHER_BEACON = register(new WatcherBeaconBlock(BlockBehaviour.Properties.of().mapColor(MapColor.BLUE).noOcclusion().sound(SoundType.NETHERITE).strength(50F).pushReaction(PushReaction.BLOCK)));
- private static Block register (Block i, String key) {
- BLOCKS_MAP.put(key, i);
- return i;
- }
- public static void register (IEventBus bus) {
- for (String key : BLOCKS_MAP.keySet())
- BLOCKS.register(key, () -> BLOCKS_MAP.get(key));
- BLOCKS.register(bus);
- }
- }
- public class WatcherBeaconBlock extends BaseEntityBlock {
- public WatcherBeaconBlock (BlockBehaviour.Properties settings) {
- super (settings);
- }
- protected MapCodec<WatcherBeaconBlock> codec () {
- return AbstractBlock.simpleCodec(WatcherBeaconBlock::new);
- }
- public WatcherBeaconBlockEntity newBlockEntity (BlockPos pos, BlockState state) {
- return new WatcherBeaconBlockEntity (pos, state);
- }
- public BlockEntityTicker<WatcherBeaconBlockEntity> getTicker (Level world, BlockState state, BlockEntityType<WatcherBeaconBlockEntity> type) {
- return (w, p, s, t) -> { t.tick(); };
- }
- public BlockState playerWillDestroy (Level world, BlockPos pos, BlockState state, Player player) {
- WatcherControl.setTarget(player, 2, pos, world);
- world.explode(player, WatcherDamagers.BEACON, new ExplosionDamageCalculator(), pos.getCenter(), 1.5F, false, Level.ExplosionInteraction.BLOCK);
- return WatcherBlocks.DESTROYED_WATCHER_BEACON.defaultBlockState();
- }
- }
- public class WatcherBeaconBlockEntity extends BlockEntity {
- private NonNullList<UUID> watchers = NonNullList.<UUID>create();
- public WatcherBeaconBlockEntity (BlockPos pos, BlockState state) {
- super (WatcherBlockEntities.BEACON, pos, state);
- }
- public void tick () {
- if (this.summonTimer >= 0) this.summonTimer--;
- if (this.summonTimer == 0) this.summon();
- if (this.watchers.isEmpty()) {
- this.refreshWatchers();
- return;
- }
- if (!this.checkWatchers()) this.broadcast();
- }
- private void refreshWatchers () {
- if (this.getLevel() == null) return;
- for (AbstractWatcher w : WatcherControl.getActiveWatchers(pos, this.getLevel()))
- this.watchers.add(w.getUUID());
- }
- private boolean checkWatchers () {
- if (this.getLevel() == null) return;
- for (UUID u : this.watchers)
- for (AbstractWatcher w : WatcherControl.getActiveWatchers(pos, this.getLevel()))
- if (u.equals(w.getUUID()))
- return true;
- return false;
- }
- private void broadcast () {
- 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) -> {
- return e instanceof Player && WatcherControl.getControlLevel((Player) e, this.getBlockPos(), this.getLevel()) > 0;
- }).getFirst();
- switch (WatcherControl.getControlLevel(player, this.getBlockPos(), this.getLevel())) {
- case 1:
- this.summonTimer = 60;
- case 2:
- this.summonTimer = 100;
- case 3:
- this.summonTimer = 200;
- case 4:
- this.summonTimer = 300;
- case 5:
- this.summonTimer = 450;
- case 6:
- this.summonTimer = 600;
- case 7:
- this.summonTimer = 900;
- case 8:
- this.summonTimer = 1200;
- case 9:
- this.summonTimer = 1800;
- default:
- return -1;
- }
- }
- private void summon () {
- 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) -> {
- return e instanceof Player && WatcherControl.getControlLevel((Player) e, this.getBlockPos(), this.getLevel()) > 0;
- }).getFirst();
- WatcherControl.summonWatchers(player, this.getBlockPos(), this.getLevel());
- }
- }
- public class WatcherControl {
- private static HashMap<PlayerHolder, Integer> targets = new HashMap<PlayerHolder, Integer>();
- public static void setTarget (Player player, int level, BlockPos pos, Level world) {
- targets.put(new PlayerHolder(player, pos, world), Integer.valueOf(level));
- }
- public static int getControlLevel (Player player, BlockPos pos, Level world) {
- Integer val = targets.get(new PlayerHolder(player, pos, level));
- if (val == null) return 0;
- else return val.intValue();
- }
- public static List<AbstractWatcher> getActiveWatchers (BlockPos pos, Level world) {
- 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(); });
- }
- public static void summonWatchers (Player target, BlockPos pos, Level world) {
- WatcherLevels level = WatcherLevels.get(getControlLevel(target, pos, world));
- for (int i = 0; i <= level.drones(); i++) doSpawn(summonWatcher(pos, world, WatcherDrone.class, WatcherEntities.DRONE), world);
- for (int i = 0; i <= level.combatDrones(); i++) summonWatcher(pos, world, WatcherCombatDrone.class, WatcherEntities.COMBAT_DRONE);
- for (int i = 0; i <= level.repairDrones(); i++) summonWatcher(pos, world, WatcherRepairDrone.class, WatcherEntities.REPAIR_DRONE);
- for (int i = 0; i <= level.heavyCombatDrones(); i++) summonWatcher(pos, world, WatcherHeavyCombatDrone.class, WatcherEntities.HEAVY_COMBAT_DRONE);
- for (int i = 0; i <= level.crawlers(); i++) summonWatcher(pos, world, WatcherCrawler.class, WatcherEntities.CRAWLER);
- for (int i = 0; i <= level.mechs(); i++) summonWatcher(pos, world, WatcherMech.class, WatcherEntities.MECH);
- for (int i = 0; i <= level.titans(); i++) summonWatcher(pos, world, WatcherTitan.class, WatcherEntities.TITAN);
- }
- public static <T extends AbstractWatcher> T summonWatcher (BlockPos pos, Level world, Class<T> clazz, EntityType<T> type) {
- while (true) {
- BlockPos summonPos;
- int x = world.random.nextIntBetweenInclusive(-15, 15);
- if (x == 0) {
- summonPos = pos;
- } else is (x < 0) {
- summonPos = pos.west(-x);
- } else {
- summonPos = pos.east(x);
- }
- int y = world.random.nextIntBetweenInclusive(-5, 5);
- if (y == 0) {
- summonPos = pos;
- } else is (y < 0) {
- summonPos = pos.below(-y);
- } else {
- summonPos = pos.above(y);
- }
- int z = world.random.nextIntBetweenInclusive(-5, 5);
- if (z == 0) {
- summonPos = pos;
- } else is (z < 0) {
- summonPos = pos.north(-z);
- } else {
- summonPos = pos.south(z);
- }
- if (world.getBlockState(summonPos).getCollisionShape(world, summonPos).isEmpty()) {
- try {
- return clazz.getConstructor(EntityType.class, Level.class).newInstance(type, world);
- } catch (NoSuchMethodException nsme) {
- throw new IllegalArgumentException("Watcher with no valid constructor passed to WatcherControl to summon", nsme);
- } catch (SecurityException se) {
- return null;
- } catch (IllegalAccessException iae) {
- throw new IllegalArgumentException("Access restricted to Watcher instantiation", iae);
- } catch (IllegalArgumentException iae2) {
- throw new IllegalStateException("Constructor with declared types does not use those types", iae2);
- } catch (InstantiationException ie) {
- throw new IllegalArgumentException("Watcher class supplied to summon is abstract and therefore uninstantiable", ie);
- } catch (InvocationTargetException ite) {
- if (ite.getCause() != null) {
- if (ite.getCause() instanceof RuntimeException) throw (RuntimeException) ite.getCause();
- if (ite.getCause() instanceof Error) throw (Error) ite.getCause();
- throw new RuntimeException("Exception thrown during Watcher instantiation", ite.getCause());
- } else {
- return null;
- }
- }
- }
- }
- }
- public static void doSpawn (AbstractWatcher watcher, Level world) {
- ServerLevelAccessor accessor;
- if (world instanceof ServerLevel sw) {
- accessor = sw;
- } else {
- accessor = world.getServer().getLevel(world.dimension());
- }
- watcher.finalizeSpawn(accessor, world.getCurrentDifficultyAt(watcher.getBlockPos()), EntitySpawnReason.TRIGGERED, null);
- }
- }
- public class AbstractWatcher extends PathfinderMob {
- public abstract boolean isActive ();
- public boolean canBeLeashed () {
- return false;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement