Hooks.java 3.2 KB

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