-
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Limits books to 50 pages with 255 characters for each. Anything outside of the limits will be automatically removed Reference: SpongePowered/Sponge#2156
- Loading branch information
Showing
5 changed files
with
144 additions
and
0 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
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
71 changes: 71 additions & 0 deletions
71
src/main/java/io/github/lxgaming/sledgehammer/mixin/core/item/MixinItemWritableBook.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,71 @@ | ||
/* | ||
* Copyright 2019 Alex Thomson | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package io.github.lxgaming.sledgehammer.mixin.core.item; | ||
|
||
import net.minecraft.item.ItemWritableBook; | ||
import net.minecraft.nbt.NBTTagCompound; | ||
import net.minecraft.nbt.NBTTagList; | ||
import net.minecraft.nbt.NBTTagString; | ||
import org.apache.commons.lang3.StringUtils; | ||
import org.spongepowered.asm.mixin.Mixin; | ||
import org.spongepowered.asm.mixin.Overwrite; | ||
|
||
@Mixin(value = ItemWritableBook.class, priority = 1337) | ||
public abstract class MixinItemWritableBook { | ||
|
||
/** | ||
* @param compound NBTTagCompound | ||
* @author LX_Gaming | ||
* @reason Limit books based on {@link net.minecraft.client.gui.GuiScreenBook GuiScreenBook} values. | ||
*/ | ||
@Overwrite | ||
public static boolean isNBTValid(NBTTagCompound compound) { | ||
// TAG_LIST | ||
if (compound == null || !compound.hasKey("pages", 9)) { | ||
return false; | ||
} | ||
|
||
// TAG_STRING | ||
NBTTagList pages = compound.getTagList("pages", 8); | ||
|
||
// net.minecraft.item.ItemWrittenBook#resolveContents(ItemStack, EntityPlayer) | ||
if (compound.getBoolean("resolved")) { | ||
for (int index = 0; index < pages.tagCount(); index++) { | ||
if (pages.getStringTagAt(index).length() > 32767) { | ||
return false; | ||
} | ||
} | ||
|
||
return true; | ||
} | ||
|
||
for (int index = 0; index < pages.tagCount(); index++) { | ||
if (index >= 50) { | ||
pages.removeTag(index); | ||
index--; | ||
continue; | ||
} | ||
|
||
String page = pages.getStringTagAt(index); | ||
if (page.length() >= 256) { | ||
pages.set(index, new NBTTagString(StringUtils.truncate(page, 252) + "...")); | ||
} | ||
} | ||
|
||
return true; | ||
} | ||
} |
62 changes: 62 additions & 0 deletions
62
...va/io/github/lxgaming/sledgehammer/mixin/core/network/MixinNetHandlerPlayServer_Book.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,62 @@ | ||
/* | ||
* Copyright 2019 Alex Thomson | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package io.github.lxgaming.sledgehammer.mixin.core.network; | ||
|
||
import net.minecraft.item.ItemStack; | ||
import net.minecraft.nbt.NBTTagCompound; | ||
import net.minecraft.network.NetHandlerPlayServer; | ||
import net.minecraft.network.PacketBuffer; | ||
import net.minecraft.network.play.client.CPacketCustomPayload; | ||
import org.spongepowered.asm.mixin.Mixin; | ||
import org.spongepowered.asm.mixin.injection.At; | ||
import org.spongepowered.asm.mixin.injection.Inject; | ||
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; | ||
import org.spongepowered.asm.mixin.injection.callback.LocalCapture; | ||
|
||
@Mixin(value = NetHandlerPlayServer.class, priority = 137) | ||
public abstract class MixinNetHandlerPlayServer_Book { | ||
|
||
@Inject(method = "processCustomPayload", | ||
at = @At(value = "INVOKE", | ||
target = "Lnet/minecraft/item/ItemWritableBook;isNBTValid(Lnet/minecraft/nbt/NBTTagCompound;)Z" | ||
), | ||
locals = LocalCapture.CAPTURE_FAILHARD | ||
) | ||
private void onIsNBTValid(CPacketCustomPayload packet, CallbackInfo callbackInfo, | ||
String channelName, PacketBuffer packetBuffer, ItemStack itemStack) { | ||
NBTTagCompound compound = itemStack.getTagCompound(); | ||
if (compound != null) { | ||
// Prevents maliciously crafted packets from bypassing our custom book validation. | ||
compound.removeTag("resolved"); | ||
} | ||
} | ||
|
||
@Inject(method = "processCustomPayload", | ||
at = @At(value = "INVOKE", | ||
target = "Lnet/minecraft/item/ItemWrittenBook;validBookTagContents(Lnet/minecraft/nbt/NBTTagCompound;)Z" | ||
), | ||
locals = LocalCapture.CAPTURE_FAILHARD | ||
) | ||
private void onValidBookTagContents(CPacketCustomPayload packet, CallbackInfo callbackInfo, | ||
String channelName, PacketBuffer packetBuffer, ItemStack itemStack) { | ||
NBTTagCompound compound = itemStack.getTagCompound(); | ||
if (compound != null) { | ||
// Prevents maliciously crafted packets from bypassing our custom book validation. | ||
compound.removeTag("resolved"); | ||
} | ||
} | ||
} |
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