Skip to content

Commit

Permalink
Generics usage in ProtocolPacketEvent for platform-dependent player
Browse files Browse the repository at this point in the history
  • Loading branch information
retrooper committed Jul 22, 2024
1 parent ea1e11d commit 6a69d1b
Show file tree
Hide file tree
Showing 8 changed files with 17 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,8 @@ public void callEvent(PacketEvent event, @Nullable Runnable postCallListenerActi
}
}
// For performance reasons, we don't want to re-encode the packet if it's not needed.
if (event instanceof ProtocolPacketEvent && !((ProtocolPacketEvent<?>) event).needsReEncode()) {
((ProtocolPacketEvent<?>) event).setLastUsedWrapper(null);
if (event instanceof ProtocolPacketEvent && !((ProtocolPacketEvent) event).needsReEncode()) {
((ProtocolPacketEvent) event).setLastUsedWrapper(null);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,14 @@

package com.github.retrooper.packetevents.event;

import com.github.retrooper.packetevents.event.simple.*;
import com.github.retrooper.packetevents.exception.PacketProcessException;
import com.github.retrooper.packetevents.manager.server.ServerVersion;
import com.github.retrooper.packetevents.netty.buffer.ByteBufHelper;
import com.github.retrooper.packetevents.protocol.PacketSide;
import com.github.retrooper.packetevents.protocol.packettype.PacketTypeCommon;
import com.github.retrooper.packetevents.protocol.player.User;

public class PacketReceiveEvent extends ProtocolPacketEvent<Object> {
public class PacketReceiveEvent extends ProtocolPacketEvent {
protected PacketReceiveEvent(Object channel, User user, Object player, Object rawByteBuf,
boolean autoProtocolTranslation) throws PacketProcessException {
super(PacketSide.CLIENT, channel, user, player, rawByteBuf, autoProtocolTranslation);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
import java.util.ArrayList;
import java.util.List;

public class PacketSendEvent extends ProtocolPacketEvent<Object> {
public class PacketSendEvent extends ProtocolPacketEvent {
private List<Runnable> tasksAfterSend = null;

protected PacketSendEvent(Object channel, User user, Object player, Object rawByteBuf,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@
* @author retrooper
* @since 1.6.9
*/
public interface PlayerEvent<T> {
public interface PlayerEvent {
/**
* Associated player.
*
* @return Player.
*/
T getPlayer();
<T> T getPlayer();
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,11 @@
import java.util.ArrayList;
import java.util.List;

public abstract class ProtocolPacketEvent<T> extends PacketEvent implements PlayerEvent<T>, CancellableEvent, UserEvent {
public abstract class ProtocolPacketEvent extends PacketEvent implements PlayerEvent, CancellableEvent, UserEvent {
private final Object channel;
private final ConnectionState connectionState;
private final User user;
private final T player;
private final Object player;
private Object byteBuf;
private final int packetID;
private final PacketTypeCommon packetType;
Expand All @@ -55,7 +55,7 @@ public abstract class ProtocolPacketEvent<T> extends PacketEvent implements Play
private boolean needsReEncode = PacketEvents.getAPI().getSettings().reEncodeByDefault();

public ProtocolPacketEvent(PacketSide packetSide, Object channel,
User user, T player, Object byteBuf,
User user, Object player, Object byteBuf,
boolean autoProtocolTranslation) throws PacketProcessException {
this.channel = channel;
this.user = user;
Expand Down Expand Up @@ -92,7 +92,7 @@ public ProtocolPacketEvent(PacketSide packetSide, Object channel,
}

public ProtocolPacketEvent(int packetID, PacketTypeCommon packetType, ServerVersion serverVersion, Object channel,
User user, T player, Object byteBuf) {
User user, Object player, Object byteBuf) {
this.channel = channel;
this.user = user;
this.player = player;
Expand Down Expand Up @@ -133,8 +133,8 @@ public User getUser() {
}

@Override
public T getPlayer() {
return player;
public <T> T getPlayer() {
return (T) player;
}

public ConnectionState getConnectionState() {
Expand Down Expand Up @@ -213,7 +213,7 @@ public boolean hasPostTasks() {
}

@Override
public ProtocolPacketEvent<?> clone() {
public ProtocolPacketEvent clone() {
return this instanceof PacketReceiveEvent ? ((PacketReceiveEvent) this).clone()
: ((PacketSendEvent) this).clone();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

import com.github.retrooper.packetevents.protocol.player.User;

public class UserLoginEvent extends PacketEvent implements CallableEvent, UserEvent, PlayerEvent<Object> {
public class UserLoginEvent extends PacketEvent implements CallableEvent, UserEvent, PlayerEvent {
private final User user;
private final Object player;

Expand All @@ -35,8 +35,8 @@ public User getUser() {
}

@Override
public Object getPlayer() {
return player;
public <T> T getPlayer() {
return (T) player;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ public void copy(T wrapper) {
//TODO public void transform(int protocolVersion) {}
//Current idea change server version, but still think more

public final void readEvent(ProtocolPacketEvent<?> event) {
public final void readEvent(ProtocolPacketEvent event) {
PacketWrapper<?> last = event.getLastUsedWrapper();
if (last != null) {
copy((T) last);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,9 @@
package io.github.retrooper.packetevents;

import com.github.retrooper.packetevents.PacketEvents;
import com.github.retrooper.packetevents.event.*;
import com.github.retrooper.packetevents.protocol.packettype.PacketType;
import com.github.retrooper.packetevents.wrapper.play.client.WrapperPlayClientChatMessage;
import com.github.retrooper.packetevents.wrapper.play.server.WrapperPlayServerChatMessage;
import com.github.retrooper.packetevents.wrapper.play.server.WrapperPlayServerKeepAlive;
import io.github.retrooper.packetevents.factory.fabric.FabricPacketEventsBuilder;
import net.fabricmc.api.ModInitializer;
import net.fabricmc.loader.api.entrypoint.PreLaunchEntrypoint;
import net.minecraft.client.Minecraft;
import net.minecraft.client.player.LocalPlayer;
import net.minecraft.network.chat.MutableComponent;
import net.minecraft.network.chat.contents.PlainTextContents;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -25,7 +16,6 @@ public class PacketEventsMod implements PreLaunchEntrypoint, ModInitializer {
@Override
public void onPreLaunch() {
PacketEvents.setAPI(FabricPacketEventsBuilder.build("packetevents"));
//PacketEvents.getAPI().getSettings().debug(true);
PacketEvents.getAPI().load();
}

Expand Down

0 comments on commit 6a69d1b

Please sign in to comment.