Skip to content

Commit 3f627d2

Browse files
Merge pull request #259 from proferabg/master
Allow custom enchantments, Fix error printed on ping, Skip decoding on invalid itemstack
2 parents d6f6232 + 99c5209 commit 3f627d2

File tree

6 files changed

+65
-29
lines changed

6 files changed

+65
-29
lines changed

protocolize-api/src/main/java/dev/simplix/protocolize/api/item/ItemStack.java

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import io.netty.buffer.Unpooled;
1010
import lombok.*;
1111
import lombok.experimental.Accessors;
12+
import lombok.extern.slf4j.Slf4j;
1213
import net.querz.nbt.tag.CompoundTag;
1314

1415
import java.util.*;
@@ -20,6 +21,7 @@
2021
*
2122
* @author Exceptionflug
2223
*/
24+
@Slf4j(topic = "Protocolize")
2325
@Getter
2426
@Setter
2527
@Accessors(fluent = true)
@@ -128,11 +130,16 @@ public ItemStack deepClone() {
128130

129131
@Override
130132
public ItemStack deepClone(int protocolVersion) {
131-
ByteBuf buf = Unpooled.buffer();
132-
ItemStackSerializer.write(buf, this, protocolVersion);
133-
ItemStack itemStack = ItemStackSerializer.read(buf, protocolVersion);
134-
buf.release();
135-
return itemStack;
133+
try {
134+
ByteBuf buf = Unpooled.buffer();
135+
ItemStackSerializer.write(buf, this, protocolVersion);
136+
ItemStack itemStack = ItemStackSerializer.read(buf, protocolVersion);
137+
buf.release();
138+
return itemStack;
139+
} catch (Exception e){
140+
log.error("Failed to clone ItemStack", e);
141+
return ItemStack.NO_DATA;
142+
}
136143
}
137144

138145
@Override

protocolize-api/src/main/java/dev/simplix/protocolize/api/item/component/EnchantmentsComponent.java

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,41 @@
11
package dev.simplix.protocolize.api.item.component;
22

33
import dev.simplix.protocolize.api.Protocolize;
4+
import dev.simplix.protocolize.api.util.Either;
45
import dev.simplix.protocolize.data.Enchantment;
56
import java.util.Map;
67

78
public interface EnchantmentsComponent extends StructuredComponent {
89

9-
Map<Enchantment, Integer> getEnchantments();
10+
Map<Either<Enchantment, Integer>, Integer> getEnchantments();
1011

11-
void setEnchantments(Map<Enchantment, Integer> enchantments);
12+
void setEnchantments(Map<Either<Enchantment, Integer>, Integer> enchantments);
1213

1314
void removeEnchantment(Enchantment enchantment);
1415

16+
void removeEnchantment(int enchantmentId);
17+
1518
void addEnchantment(Enchantment enchantment, int level);
1619

20+
void addEnchantment(int enchantmentId, int level);
21+
1722
void setShowInTooltip(boolean show);
1823

1924
void removeAllEnchantments();
2025

21-
static EnchantmentsComponent create(Map<Enchantment, Integer> enchantments) {
26+
static EnchantmentsComponent create(Map<Either<Enchantment, Integer>, Integer> enchantments) {
2227
return Protocolize.getService(Factory.class).create(enchantments);
2328
}
2429

25-
static EnchantmentsComponent create(Map<Enchantment, Integer> enchantments, boolean showInTooltip) {
30+
static EnchantmentsComponent create(Map<Either<Enchantment, Integer>, Integer> enchantments, boolean showInTooltip) {
2631
return Protocolize.getService(Factory.class).create(enchantments, showInTooltip);
2732
}
2833

2934
interface Factory {
3035

31-
EnchantmentsComponent create(Map<Enchantment, Integer> enchantments);
36+
EnchantmentsComponent create(Map<Either<Enchantment, Integer>, Integer> enchantments);
3237

33-
EnchantmentsComponent create(Map<Enchantment, Integer> enchantments, boolean showInTooltip);
38+
EnchantmentsComponent create(Map<Either<Enchantment, Integer>, Integer> enchantments, boolean showInTooltip);
3439

3540
}
3641

protocolize-api/src/main/java/dev/simplix/protocolize/api/item/component/StoredEnchantmentsComponent.java

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,42 @@
11
package dev.simplix.protocolize.api.item.component;
22

33
import dev.simplix.protocolize.api.Protocolize;
4+
import dev.simplix.protocolize.api.util.Either;
45
import dev.simplix.protocolize.data.Enchantment;
56

67
import java.util.Map;
78

89
public interface StoredEnchantmentsComponent extends StructuredComponent {
910

10-
Map<Enchantment, Integer> getEnchantments();
11+
Map<Either<Enchantment, Integer>, Integer> getEnchantments();
1112

12-
void setEnchantments(Map<Enchantment, Integer> enchantments);
13+
void setEnchantments(Map<Either<Enchantment, Integer>, Integer> enchantments);
1314

1415
void removeEnchantment(Enchantment enchantment);
1516

17+
void removeEnchantment(int enchantmentId);
18+
1619
void addEnchantment(Enchantment enchantment, int level);
1720

21+
void addEnchantment(int enchantmentId, int level);
22+
1823
void setShowInTooltip(boolean show);
1924

2025
void removeAllEnchantments();
2126

22-
static StoredEnchantmentsComponent create(Map<Enchantment, Integer> enchantments) {
27+
static StoredEnchantmentsComponent create(Map<Either<Enchantment, Integer>, Integer> enchantments) {
2328
return Protocolize.getService(Factory.class).create(enchantments);
2429
}
2530

26-
static StoredEnchantmentsComponent create(Map<Enchantment, Integer> enchantments, boolean showInTooltip) {
31+
static StoredEnchantmentsComponent create(Map<Either<Enchantment, Integer>, Integer> enchantments, boolean showInTooltip) {
2732
return Protocolize.getService(Factory.class).create(enchantments, showInTooltip);
2833
}
2934

3035
interface Factory {
3136

32-
StoredEnchantmentsComponent create(Map<Enchantment, Integer> enchantments);
37+
StoredEnchantmentsComponent create(Map<Either<Enchantment, Integer>, Integer> enchantments);
3338

34-
StoredEnchantmentsComponent create(Map<Enchantment, Integer> enchantments, boolean showInTooltip);
39+
StoredEnchantmentsComponent create(Map<Either<Enchantment, Integer>, Integer> enchantments, boolean showInTooltip);
3540

3641
}
3742

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package dev.simplix.protocolize.api.item.component.exception;
2+
3+
public class InvalidDataComponentTypeException extends IllegalStateException {
4+
public InvalidDataComponentTypeException(int componentId, int protocolVersion) {
5+
super("Could not find component type " + componentId + " for protocol version " +
6+
protocolVersion + ". This item is not compatible with Protocolize.");
7+
}
8+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package dev.simplix.protocolize.api.item.component.exception;
2+
3+
import dev.simplix.protocolize.api.item.component.StructuredComponentType;
4+
5+
public class InvalidDataComponentVersionException extends IllegalStateException {
6+
public InvalidDataComponentVersionException(StructuredComponentType<?> componentType, int protocolVersion) {
7+
super("Component " + componentType.getName() + " can not be used on protocol version " + protocolVersion);
8+
}
9+
}

protocolize-velocity/src/main/java/dev/simplix/protocolize/velocity/listener/PlayerListener.java

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public PlayerListener(ProtocolizePlugin plugin) {
5959
@Subscribe
6060
public void onPing(ProxyPingEvent event) {
6161
try {
62-
initConnection(event.getConnection());
62+
initConnection(event.getConnection(), true);
6363
} catch (Exception e) {
6464
log.error("Unable to initialize InboundConnection on ping", e);
6565
}
@@ -68,7 +68,7 @@ public void onPing(ProxyPingEvent event) {
6868
@Subscribe
6969
public void onPreLogin(LoginEvent event) {
7070
try {
71-
initConnection(event.getPlayer());
71+
initConnection(event.getPlayer(), false);
7272
} catch (Exception e) {
7373
log.error("Unable to initialize InboundConnection on pre login", e);
7474
}
@@ -88,7 +88,7 @@ public void onDisconnect(DisconnectEvent event) {
8888
PLAYER_PROVIDER.playerDisconnect(event.getPlayer().getUniqueId());
8989
}
9090

91-
private void initConnection(InboundConnection connection) throws ReflectiveOperationException {
91+
private void initConnection(InboundConnection connection, boolean isPing) throws ReflectiveOperationException {
9292
PipelineAccessor accessor = PIPELINE_ACCESSOR_MAP.get(connection.getClass().getName());
9393
if (accessor == null) {
9494
throw new UnsupportedOperationException("InboundConnection of type " + connection.getClass().getName()
@@ -102,15 +102,17 @@ private void initConnection(InboundConnection connection) throws ReflectiveOpera
102102
if (decoderChannelHandler == null) {
103103
// Ah yes expecting an overridden channel initializer
104104
ChannelInitializer<Channel> initializer = plugin.currentBackendChannelInitializer();
105-
if (initializer.getClass() != ProtocolizeBackendChannelInitializer.class) {
106-
log.error("It seems like there is an incompatible plugin installed. Velocity channel initializers are overridden by: {}", initializer.getClass().getName());
107-
log.error("Protocolize is unable to work under this circumstances. Please contact the developers of the incompatible " +
108-
"plugin and suggest them to call the initChannel method of the ChannelInitializers before overriding them.");
109-
return;
110-
} else {
111-
// WTF??
112-
log.error("Pipeline is not initialized. This is a bug. Please report. Pipeline handlers = {}", pipeline.toMap());
113-
log.error("Initializer: {}", initializer.getClass().getName());
105+
if(!isPing) {
106+
if (initializer.getClass() != ProtocolizeBackendChannelInitializer.class) {
107+
log.error("It seems like there is an incompatible plugin installed. Velocity channel initializers are overridden by: {}", initializer.getClass().getName());
108+
log.error("Protocolize is unable to work under this circumstances. Please contact the developers of the incompatible " +
109+
"plugin and suggest them to call the initChannel method of the ChannelInitializers before overriding them.");
110+
return;
111+
} else {
112+
// WTF??
113+
log.error("Pipeline is not initialized. This is a bug. Please report. Pipeline handlers = {}", pipeline.toMap());
114+
log.error("Initializer: {}", initializer.getClass().getName());
115+
}
114116
}
115117
return;
116118
}

0 commit comments

Comments
 (0)