|
| 1 | +package com.minecrafttas.tasmod.savestates.handlers; |
| 2 | + |
| 3 | +import java.nio.ByteBuffer; |
| 4 | +import java.nio.file.Path; |
| 5 | +import java.util.List; |
| 6 | +import java.util.concurrent.CompletableFuture; |
| 7 | +import java.util.concurrent.ExecutionException; |
| 8 | +import java.util.concurrent.TimeUnit; |
| 9 | +import java.util.concurrent.TimeoutException; |
| 10 | + |
| 11 | +import com.minecrafttas.mctcommon.networking.Client.Side; |
| 12 | +import com.minecrafttas.mctcommon.networking.exception.PacketNotImplementedException; |
| 13 | +import com.minecrafttas.mctcommon.networking.exception.WrongSideException; |
| 14 | +import com.minecrafttas.mctcommon.networking.interfaces.ClientPacketHandler; |
| 15 | +import com.minecrafttas.mctcommon.networking.interfaces.PacketID; |
| 16 | +import com.minecrafttas.mctcommon.networking.interfaces.ServerPacketHandler; |
| 17 | +import com.minecrafttas.tasmod.TASmod; |
| 18 | +import com.minecrafttas.tasmod.TASmodClient; |
| 19 | +import com.minecrafttas.tasmod.events.EventSavestate; |
| 20 | +import com.minecrafttas.tasmod.networking.TASmodBufferBuilder; |
| 21 | +import com.minecrafttas.tasmod.registries.TASmodPackets; |
| 22 | +import com.minecrafttas.tasmod.savestates.exceptions.SavestateException; |
| 23 | +import com.minecrafttas.tasmod.util.Ducks.ResourcePackRepositoryDuck; |
| 24 | +import com.minecrafttas.tasmod.util.LoggerMarkers; |
| 25 | + |
| 26 | +import net.minecraft.client.Minecraft; |
| 27 | +import net.minecraft.entity.player.EntityPlayerMP; |
| 28 | +import net.minecraft.server.MinecraftServer; |
| 29 | + |
| 30 | +public class SavestateResourcePackHandler implements EventSavestate.EventServerLoadstate, ServerPacketHandler, ClientPacketHandler { |
| 31 | + |
| 32 | + private CompletableFuture<String> future; |
| 33 | + |
| 34 | + @Override |
| 35 | + public void onServerLoadstate(MinecraftServer server, int index, Path target, Path current) { |
| 36 | + if (server.getResourcePackUrl().isEmpty() || server.isDedicatedServer()) |
| 37 | + return; |
| 38 | + |
| 39 | + String serverOwnerName = server.getServerOwner(); |
| 40 | + |
| 41 | + try { |
| 42 | + TASmod.server.sendTo(serverOwnerName, new TASmodBufferBuilder(TASmodPackets.SAVESTATE_CLEAR_RESOURCEPACK)); |
| 43 | + } catch (Exception e) { |
| 44 | + TASmod.LOGGER.catching(e); |
| 45 | + } |
| 46 | + future = new CompletableFuture<>(); |
| 47 | + |
| 48 | + String playername = null; |
| 49 | + try { |
| 50 | + playername = future.get(2L, TimeUnit.MINUTES); |
| 51 | + } catch (TimeoutException e) { |
| 52 | + throw new SavestateException(e, "Clearing resourcepacks %s timed out!", serverOwnerName); |
| 53 | + } catch (ExecutionException | InterruptedException e) { |
| 54 | + throw new SavestateException(e, "Clearing resourcepacks %s", serverOwnerName); |
| 55 | + } |
| 56 | + |
| 57 | + TASmod.LOGGER.debug(LoggerMarkers.Savestate, "Cleared resourcepack for player {}", playername); |
| 58 | + } |
| 59 | + |
| 60 | + @Override |
| 61 | + public PacketID[] getAcceptedPacketIDs() { |
| 62 | + return new TASmodPackets[] { TASmodPackets.SAVESTATE_CLEAR_RESOURCEPACK }; |
| 63 | + } |
| 64 | + |
| 65 | + @Override |
| 66 | + public void onClientPacket(PacketID id, ByteBuffer buf, String username) throws PacketNotImplementedException, WrongSideException, Exception { |
| 67 | + TASmodPackets packetId = (TASmodPackets) id; |
| 68 | + |
| 69 | + Minecraft mc = Minecraft.getMinecraft(); |
| 70 | + switch (packetId) { |
| 71 | + case SAVESTATE_CLEAR_RESOURCEPACK: |
| 72 | + mc.addScheduledTask(() -> { |
| 73 | + ResourcePackRepositoryDuck duck = (ResourcePackRepositoryDuck) mc.getResourcePackRepository(); |
| 74 | + duck.clearServerResourcePackBlocking(); |
| 75 | + try { |
| 76 | + TASmodClient.client.send(new TASmodBufferBuilder(TASmodPackets.SAVESTATE_CLEAR_RESOURCEPACK)); |
| 77 | + } catch (Exception e) { |
| 78 | + TASmod.LOGGER.catching(e); |
| 79 | + } |
| 80 | + }); |
| 81 | + break; |
| 82 | + |
| 83 | + default: |
| 84 | + throw new WrongSideException(packetId, Side.CLIENT); |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + @Override |
| 89 | + public void onServerPacket(PacketID id, ByteBuffer buf, String username) throws PacketNotImplementedException, WrongSideException, Exception { |
| 90 | + TASmodPackets packetId = (TASmodPackets) id; |
| 91 | + |
| 92 | + switch (packetId) { |
| 93 | + case SAVESTATE_CLEAR_RESOURCEPACK: |
| 94 | + future.complete(username); |
| 95 | + break; |
| 96 | + |
| 97 | + default: |
| 98 | + throw new WrongSideException(packetId, Side.SERVER); |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + public static void refreshServerResourcepack(MinecraftServer server) { |
| 103 | + List<EntityPlayerMP> players = server.getPlayerList().getPlayers(); |
| 104 | + players.forEach((player) -> { |
| 105 | + player.loadResourcePack(server.getResourcePackUrl(), server.getResourcePackHash()); |
| 106 | + }); |
| 107 | + } |
| 108 | +} |
0 commit comments