Hooks.java 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. package me.kcm;
  2. import java.util.Collections;
  3. import java.util.List;
  4. import java.util.function.Consumer;
  5. import javax.annotation.Nullable;
  6. import net.minecraft.block.BlockState;
  7. import net.minecraft.entity.Entity;
  8. import net.minecraft.entity.player.PlayerEntity;
  9. import net.minecraft.inventory.CraftResultInventory;
  10. import net.minecraft.inventory.CraftingInventory;
  11. import net.minecraft.item.ItemStack;
  12. import net.minecraft.server.dedicated.DedicatedPlayerList;
  13. import net.minecraft.server.dedicated.DedicatedServer;
  14. import net.minecraft.tileentity.TileEntity;
  15. import net.minecraft.util.math.BlockPos;
  16. import net.minecraft.util.registry.DynamicRegistries;
  17. import net.minecraft.world.World;
  18. import net.minecraft.world.server.ServerWorld;
  19. import net.minecraft.world.storage.PlayerData;
  20. public class Hooks {
  21. @FunctionalInterface
  22. public interface PlayerListFunction {
  23. public void accept(DedicatedServer server, DynamicRegistries.Impl impl, PlayerData pd);
  24. }
  25. private static PlayerListFunction playerListFunction = null;
  26. private static BlockHarvest blockHarvest = (state, w, pos, tileEnt, ent, stack) -> Collections.EMPTY_LIST;
  27. private static Craft craft = (id, w, p, cInv, slot) -> {
  28. };
  29. public static void setPlayerListFunction(PlayerListFunction c) {
  30. playerListFunction = c;
  31. }
  32. public static void setPlayerList(DedicatedServer server, DynamicRegistries.Impl impl, PlayerData pd) {
  33. System.out.println("trying to update player list ...");
  34. if(playerListFunction != null) {
  35. playerListFunction.accept(server, impl, pd);
  36. System.out.println("succeeded with custom function");
  37. } else {
  38. try {
  39. server.setPlayerList(new DedicatedPlayerList(server, impl, pd));
  40. System.out.println("succeeded with vanilla function");
  41. } catch(Exception ex) {
  42. // this is stupid and should not be needed
  43. ex.printStackTrace();
  44. }
  45. }
  46. }
  47. public static void setBlockHarvest(BlockHarvest c) {
  48. blockHarvest = c;
  49. }
  50. public static void setCraft(Craft c) {
  51. craft = c;
  52. }
  53. public static List<ItemStack> getDropsA(BlockState state, ServerWorld w, BlockPos pos, @Nullable TileEntity tileEnt) {
  54. return blockHarvest.onBlockHarvest(state, w, pos, tileEnt, null, null);
  55. }
  56. public static List<ItemStack> getDropsB(BlockState state, ServerWorld w, BlockPos pos, @Nullable TileEntity tileEnt, @Nullable Entity ent, ItemStack stack) {
  57. return blockHarvest.onBlockHarvest(state, w, pos, tileEnt, ent, stack);
  58. }
  59. public static void onCraft(int id, World w, PlayerEntity p, CraftingInventory cInv, CraftResultInventory slot) {
  60. craft.onCraft(id, w, p, cInv, slot);
  61. }
  62. }