Advertisement
Guest User

Custom

a guest
Sep 11th, 2023
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.50 KB | None | 0 0
  1. package net.coderandom.etheriacraft.procedures;
  2.  
  3. import net.minecraft.resources.ResourceLocation;
  4. import net.minecraft.util.RandomSource;
  5. import net.minecraft.world.level.Level;
  6. import net.minecraft.world.item.ItemStack;
  7. import net.minecraft.world.entity.Entity;
  8. import net.minecraft.world.effect.MobEffectInstance;
  9. import net.minecraft.server.level.ServerPlayer;
  10. import net.minecraft.server.level.ServerLevel;
  11. import net.minecraft.resources.ResourceKey;
  12. import net.minecraft.network.protocol.game.ClientboundUpdateMobEffectPacket;
  13. import net.minecraft.network.protocol.game.ClientboundPlayerAbilitiesPacket;
  14. import net.minecraft.network.protocol.game.ClientboundLevelEventPacket;
  15. import net.minecraft.network.protocol.game.ClientboundGameEventPacket;
  16. import net.minecraft.core.BlockPos;
  17. import net.coderandom.etheriacraft.EtheriaCraftMod;
  18.  
  19. public class FocusedEyeOfEnderRightClickedCUSTOMProcedure {
  20.  
  21. public static double execute(Entity entity, ItemStack itemstack) {
  22. if (entity == null) {
  23. return 0;
  24. }
  25.  
  26. if (entity.isShiftKeyDown()) {
  27. saveLocationData(entity, itemstack);
  28. return 1;
  29. }
  30.  
  31. if (itemstack.getOrCreateTag().getBoolean("canTeleport")) {
  32. if (isDimensionChanged(entity, itemstack)) {
  33. handleDimensionChange(entity, itemstack);
  34. }
  35. applyRotationAndTeleport(entity, itemstack);
  36. damageItem(itemstack);
  37. }
  38. return 0;
  39. }
  40.  
  41. private static void saveLocationData(Entity entity, ItemStack itemstack) {
  42. itemstack.getOrCreateTag().putString("dimension", entity.level().dimension().location().toString());
  43. itemstack.getOrCreateTag().putDouble("teleportX", entity.getX());
  44. itemstack.getOrCreateTag().putDouble("teleportY", entity.getY());
  45. itemstack.getOrCreateTag().putDouble("teleportZ", entity.getZ());
  46. itemstack.getOrCreateTag().putDouble("yaw", entity.getYRot());
  47. itemstack.getOrCreateTag().putDouble("pitch", entity.getXRot());
  48. itemstack.getOrCreateTag().putBoolean("canTeleport", true);
  49. EtheriaCraftMod.LOGGER.debug(entity.level().dimension());
  50. }
  51.  
  52. private static boolean isDimensionChanged(Entity entity, ItemStack itemstack) {
  53. return !itemstack.getOrCreateTag().getString("dimension").equals(entity.level().dimension().location().toString());
  54. }
  55.  
  56. private static void handleDimensionChange(Entity entity, ItemStack itemstack) {
  57. String dimensionTag = itemstack.getOrCreateTag().getString("dimension");
  58. if (entity instanceof ServerPlayer serverPlayer) {
  59. ServerLevel nextLevel = null;
  60. if (dimensionTag.contains("minecraft:overworld")) {
  61. nextLevel = serverPlayer.server.getLevel(Level.OVERWORLD);
  62. } else if (dimensionTag.contains("minecraft:the_nether")) {
  63. nextLevel = serverPlayer.server.getLevel(Level.NETHER);
  64. } else if (dimensionTag.contains("minecraft:the_end")) {
  65. nextLevel = serverPlayer.server.getLevel(Level.END);
  66. }
  67.  
  68. if (nextLevel != null) {
  69. serverPlayer.connection.send(new ClientboundGameEventPacket(ClientboundGameEventPacket.WIN_GAME, 0));
  70. serverPlayer.teleportTo(nextLevel, serverPlayer.getX(), serverPlayer.getY(), serverPlayer.getZ(), serverPlayer.getYRot(), serverPlayer.getXRot());
  71. serverPlayer.connection.send(new ClientboundPlayerAbilitiesPacket(serverPlayer.getAbilities()));
  72. for (MobEffectInstance effectInstance : serverPlayer.getActiveEffects()) {
  73. serverPlayer.connection.send(new ClientboundUpdateMobEffectPacket(serverPlayer.getId(), effectInstance));
  74. }
  75. serverPlayer.connection.send(new ClientboundLevelEventPacket(1032, BlockPos.ZERO, 0, false));
  76. }
  77. }
  78. }
  79.  
  80.  
  81. private static void applyRotationAndTeleport(Entity entity, ItemStack itemstack) {
  82. entity.setYRot((float) itemstack.getOrCreateTag().getDouble("yaw"));
  83. entity.setXRot((float) itemstack.getOrCreateTag().getDouble("pitch"));
  84. entity.setYBodyRot(entity.getYRot());
  85. entity.setYHeadRot(entity.getYRot());
  86. entity.yRotO = entity.getYRot();
  87. entity.xRotO = entity.getXRot();
  88.  
  89. entity.teleportTo(itemstack.getOrCreateTag().getDouble("teleportX"), itemstack.getOrCreateTag().getDouble("teleportY"), itemstack.getOrCreateTag().getDouble("teleportZ"));
  90. if (entity instanceof ServerPlayer player) {
  91. player.connection.teleport(itemstack.getOrCreateTag().getDouble("teleportX"), itemstack.getOrCreateTag().getDouble("teleportY"), itemstack.getOrCreateTag().getDouble("teleportZ"), entity.getYRot(), entity.getXRot());
  92. }
  93. }
  94.  
  95. private static void damageItem(ItemStack itemstack) {
  96. if (itemstack.hurt(1, RandomSource.create(), null)) {
  97. itemstack.shrink(1);
  98. itemstack.setDamageValue(0);
  99. }
  100. }
  101. }
  102.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement