-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
358 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
src/main/java/net/mcbrawls/packmanager/ResourcePackEnvironment.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package net.mcbrawls.packmanager; | ||
|
||
import net.minecraft.network.packet.s2c.common.ResourcePackRemoveS2CPacket; | ||
import net.minecraft.network.packet.s2c.common.ResourcePackSendS2CPacket; | ||
import net.minecraft.server.MinecraftServer.ServerResourcePackProperties; | ||
|
||
import java.util.Optional; | ||
import java.util.Set; | ||
import java.util.UUID; | ||
|
||
/** | ||
* Represents a context which can manage resource packs. | ||
*/ | ||
public interface ResourcePackEnvironment { | ||
/** | ||
* Adds a resource pack to this environment. | ||
* @param properties the resource pack properties | ||
*/ | ||
default void addResourcePack(ServerResourcePackProperties properties) { | ||
throw new AssertionError("Not implemented in mixin"); | ||
} | ||
|
||
/** | ||
* Removes a resource pack from this environment. | ||
* @param uuid the uuid of the resource pack | ||
*/ | ||
default void removeResourcePack(UUID uuid) { | ||
throw new AssertionError("Not implemented in mixin"); | ||
} | ||
|
||
/** | ||
* Returns a set of all resource packs within this environment. | ||
* @return a set of resource pack properties | ||
*/ | ||
default Set<ServerResourcePackProperties> getResourcePacks() { | ||
throw new AssertionError("Not implemented in mixin"); | ||
} | ||
|
||
/** | ||
* Creates a packet which enables a resource pack for the given properties. | ||
* @return a resource pack send packet | ||
*/ | ||
static ResourcePackSendS2CPacket createSendPacket(ServerResourcePackProperties properties) { | ||
return new ResourcePackSendS2CPacket( | ||
properties.id(), | ||
properties.url(), | ||
properties.hash(), | ||
properties.isRequired(), | ||
properties.prompt() | ||
); | ||
} | ||
|
||
/** | ||
* Creates a packet which removes a resource pack for the given properties. | ||
* @param uuid the uuid of the resource pack to be removed | ||
* @return a resource pack remove packet | ||
*/ | ||
static ResourcePackRemoveS2CPacket createRemovePacket(UUID uuid) { | ||
return new ResourcePackRemoveS2CPacket(Optional.of(uuid)); | ||
} | ||
} |
60 changes: 60 additions & 0 deletions
60
src/main/java/net/mcbrawls/packmanager/mixin/MinecraftServerMixin.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package net.mcbrawls.packmanager.mixin; | ||
|
||
import net.mcbrawls.packmanager.ResourcePackEnvironment; | ||
import net.minecraft.server.MinecraftServer; | ||
import net.minecraft.server.MinecraftServer.ServerResourcePackProperties; | ||
import net.minecraft.server.PlayerManager; | ||
import org.spongepowered.asm.mixin.Mixin; | ||
import org.spongepowered.asm.mixin.Shadow; | ||
import org.spongepowered.asm.mixin.Unique; | ||
|
||
import java.util.HashMap; | ||
import java.util.HashSet; | ||
import java.util.Map; | ||
import java.util.Set; | ||
import java.util.UUID; | ||
|
||
/** | ||
* Transforms MinecraftServer into a resource pack environment. | ||
*/ | ||
@Mixin(MinecraftServer.class) | ||
public class MinecraftServerMixin implements ResourcePackEnvironment { | ||
@Shadow private PlayerManager playerManager; | ||
|
||
/** | ||
* The active resource packs on the server. | ||
*/ | ||
@Unique | ||
private final Map<UUID, ServerResourcePackProperties> resourcePacks = new HashMap<>(); | ||
|
||
@Unique | ||
@Override | ||
public void addResourcePack(ServerResourcePackProperties properties) { | ||
// try add resource pack | ||
if (resourcePacks.put(properties.id(), properties) == null) { | ||
// send pack to players if accepted | ||
if (this.playerManager != null) { | ||
var packet = ResourcePackEnvironment.createSendPacket(properties); | ||
this.playerManager.sendToAll(packet); | ||
} | ||
} | ||
} | ||
|
||
@Unique | ||
@Override | ||
public void removeResourcePack(UUID uuid) { | ||
// try remove resource pack | ||
if (resourcePacks.remove(uuid) != null) { | ||
// send pack removal to players if accepted | ||
if (this.playerManager != null) { | ||
var packet = ResourcePackEnvironment.createRemovePacket(uuid); | ||
this.playerManager.sendToAll(packet); | ||
} | ||
} | ||
} | ||
|
||
@Override | ||
public Set<ServerResourcePackProperties> getResourcePacks() { | ||
return new HashSet<>(resourcePacks.values()); | ||
} | ||
} |
50 changes: 50 additions & 0 deletions
50
src/main/java/net/mcbrawls/packmanager/mixin/ServerWorldMixin.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,59 @@ | ||
package net.mcbrawls.packmanager.mixin; | ||
|
||
import net.mcbrawls.packmanager.ResourcePackEnvironment; | ||
import net.minecraft.server.MinecraftServer.ServerResourcePackProperties; | ||
import net.minecraft.server.network.ServerPlayerEntity; | ||
import net.minecraft.server.world.ServerWorld; | ||
import org.spongepowered.asm.mixin.Final; | ||
import org.spongepowered.asm.mixin.Mixin; | ||
import org.spongepowered.asm.mixin.Shadow; | ||
import org.spongepowered.asm.mixin.Unique; | ||
|
||
import java.util.HashMap; | ||
import java.util.HashSet; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Set; | ||
import java.util.UUID; | ||
|
||
/** | ||
* Transforms ServerWorld into a resource pack environment. | ||
*/ | ||
@Mixin(ServerWorld.class) | ||
public class ServerWorldMixin implements ResourcePackEnvironment { | ||
@Shadow @Final | ||
List<ServerPlayerEntity> players; | ||
|
||
/** | ||
* The active resource packs on the world. | ||
*/ | ||
@Unique | ||
private final Map<UUID, ServerResourcePackProperties> resourcePacks = new HashMap<>(); | ||
|
||
@Unique | ||
@Override | ||
public void addResourcePack(ServerResourcePackProperties properties) { | ||
// try add resource pack | ||
if (resourcePacks.put(properties.id(), properties) == null) { | ||
// send pack to players if accepted | ||
var packet = ResourcePackEnvironment.createSendPacket(properties); | ||
this.players.forEach(player -> player.networkHandler.sendPacket(packet)); | ||
} | ||
} | ||
|
||
@Unique | ||
@Override | ||
public void removeResourcePack(UUID uuid) { | ||
// try remove resource pack | ||
if (resourcePacks.remove(uuid) != null) { | ||
// send pack removal to players if accepted | ||
var packet = ResourcePackEnvironment.createRemovePacket(uuid); | ||
this.players.forEach(player -> player.networkHandler.sendPacket(packet)); | ||
} | ||
} | ||
|
||
@Override | ||
public Set<ServerResourcePackProperties> getResourcePacks() { | ||
return new HashSet<>(resourcePacks.values()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 0 additions & 8 deletions
8
src/main/kotlin/net/mcbrawls/packmanager/ResourcePackEnvironment.kt
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.