Skip to content

Commit

Permalink
General fixes
Browse files Browse the repository at this point in the history
- Fixed resource packs creation
- It now works better on client
  • Loading branch information
Patbox committed Jun 20, 2021
1 parent 576a308 commit e303917
Show file tree
Hide file tree
Showing 10 changed files with 175 additions and 5 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ org.gradle.jvmargs=-Xmx1G
loader_version=0.11.3

# Mod Properties
mod_version = 0.1.0-pre3-1.17
mod_version = 0.1.0-pre4-1.17
maven_group = eu.pb4
archives_base_name = polymer

Expand Down
12 changes: 12 additions & 0 deletions src/main/java/eu/pb4/polymer/block/VirtualBlock.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import net.minecraft.block.BlockState;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;


/**
Expand All @@ -18,6 +19,17 @@ public interface VirtualBlock extends VirtualObject {
*/
Block getVirtualBlock();

/**
* Returns block used on client for provided location;
* It's used to validate blocks
*
* @return Vanilla (or other) Block instance
*/
default Block getVirtualBlock(BlockPos pos, World world) {
return this.getVirtualBlock();
}


/**
* Returns default virtual BlockState
*
Expand Down
5 changes: 4 additions & 1 deletion src/main/java/eu/pb4/polymer/item/ItemHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import net.minecraft.nbt.NbtString;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.text.LiteralText;
import net.minecraft.text.MutableText;
import net.minecraft.text.Style;
import net.minecraft.text.Text;
import net.minecraft.util.Formatting;
Expand Down Expand Up @@ -141,7 +142,9 @@ public static ItemStack createBasicVirtualItemStack(ItemStack itemStack, @Nullab
}

List<Text> tooltip = itemStack.getTooltip(player, TooltipContext.Default.NORMAL);
out.setCustomName(tooltip.remove(0));
MutableText name = tooltip.remove(0).shallowCopy();
name.setStyle(name.getStyle().withParent(NON_ITALIC_STYLE));
out.setCustomName(name);

if (itemStack.getItem() instanceof VirtualItem) {
((VirtualItem) itemStack.getItem()).modifyTooltip(tooltip, itemStack, player);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package eu.pb4.polymer.mixin.block;

import eu.pb4.polymer.block.VirtualBlock;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.minecraft.block.Block;
import net.minecraft.client.MinecraftClient;
import net.minecraft.network.PacketByteBuf;
import net.minecraft.network.packet.s2c.play.BlockEventS2CPacket;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.util.math.BlockPos;
import org.spongepowered.asm.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.CallbackInfoReturnable;
import xyz.nucleoid.packettweaker.PacketContext;

@Mixin(BlockEventS2CPacket.class)
public class BlockEventS2CPacketMixin {
@Shadow @Mutable
private Block block;

@Shadow @Final private BlockPos pos;

@Unique private Block oldBlock = null;

@Environment(EnvType.CLIENT)
@Inject(method = "getBlock", at = @At("TAIL"), cancellable = true)
private void replaceBlockClient(CallbackInfoReturnable<Block> cir) {
ServerPlayerEntity player = MinecraftClient.getInstance().getServer().getPlayerManager().getPlayer(MinecraftClient.getInstance().player.getUuid());
if (this.oldBlock instanceof VirtualBlock virtualBlock) {
cir.setReturnValue(virtualBlock.getVirtualBlock(this.pos, player.getServerWorld()));
}
}

@Inject(method = "write", at = @At("TAIL"))
private void replaceBlock(PacketByteBuf byteBuf, CallbackInfo ci) {
if (oldBlock == null) {
this.oldBlock = block;
}

if (this.oldBlock instanceof VirtualBlock virtualBlock) {
this.block = virtualBlock.getVirtualBlock(this.pos, PacketContext.get().getTarget().getServerWorld());
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package eu.pb4.polymer.mixin.block;

import eu.pb4.polymer.block.VirtualBlock;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.minecraft.block.BlockState;
import net.minecraft.network.packet.s2c.play.PlayerActionResponseS2CPacket;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.ModifyArg;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;

@Mixin(PlayerActionResponseS2CPacket.class)
public class PlayerActionResponseS2CPacketMixin {
@Shadow @Final private BlockState state;

@ModifyArg(method = "write", at = @At(value = "INVOKE", target = "Lnet/minecraft/block/Block;getRawIdFromState(Lnet/minecraft/block/BlockState;)I"))
private BlockState replaceWithVirtualBlockState(BlockState state) {
if (state.getBlock() instanceof VirtualBlock) {
return ((VirtualBlock) state.getBlock()).getVirtualBlockState(state);
}
return state;
}


@Environment(EnvType.CLIENT)
@Inject(method = "getBlockState", at = @At("HEAD"), cancellable = true)
public void replaceWithVirtualState(CallbackInfoReturnable<BlockState> cir) {
if (this.state.getBlock() instanceof VirtualBlock virtualBlock) {
cir.setReturnValue(virtualBlock.getVirtualBlockState(this.state));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package eu.pb4.polymer.mixin.item;

import eu.pb4.polymer.item.ItemHelper;
import it.unimi.dsi.fastutil.ints.Int2ObjectArrayMap;
import it.unimi.dsi.fastutil.ints.Int2ObjectMap;
import net.minecraft.item.ItemStack;
import net.minecraft.network.packet.c2s.play.ClickSlotC2SPacket;
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.CallbackInfoReturnable;

@Mixin(ClickSlotC2SPacket.class)
public class ClickSlotC2SPacketMixin {
@Inject(method = "getStack", at = @At("TAIL"), cancellable = true)
private void replaceWithReal(CallbackInfoReturnable<ItemStack> cir) {
cir.setReturnValue(ItemHelper.getRealItemStack(cir.getReturnValue()));
}

@Inject(method = "getModifiedStacks", at = @At("TAIL"), cancellable = true)
private void replaceMultipleReal(CallbackInfoReturnable<Int2ObjectMap<ItemStack>> cir) {
Int2ObjectMap map = new Int2ObjectArrayMap();

for (Int2ObjectMap.Entry<ItemStack> entry : cir.getReturnValue().int2ObjectEntrySet()) {
map.put(entry.getIntKey(), ItemHelper.getRealItemStack(entry.getValue()));
}

cir.setReturnValue(map);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package eu.pb4.polymer.mixin.item;

import eu.pb4.polymer.item.ItemHelper;
import net.minecraft.item.ItemStack;
import net.minecraft.network.packet.c2s.play.CreativeInventoryActionC2SPacket;
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.CallbackInfoReturnable;

@Mixin(CreativeInventoryActionC2SPacket.class)
public class CreativeInventoryActionC2SPacketMixin {
@Inject(method = "getItemStack", at = @At("TAIL"), cancellable = true)
private void replaceWithReal(CallbackInfoReturnable<ItemStack> cir) {
cir.setReturnValue(ItemHelper.getRealItemStack(cir.getReturnValue()));
}
}
16 changes: 16 additions & 0 deletions src/main/java/eu/pb4/polymer/mixin/item/ItemStackMixin.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package eu.pb4.polymer.mixin.item;

import eu.pb4.polymer.item.ItemHelper;
import net.minecraft.item.ItemStack;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.ModifyArg;

@Mixin(ItemStack.class)
public class ItemStackMixin {
@ModifyArg(method = "getTooltip", at = @At(value = "INVOKE", target = "Lnet/minecraft/text/LiteralText;<init>(Ljava/lang/String;)V", ordinal = 3))
private String changeId(String id) {
ItemStack stack = (ItemStack) (Object) this;
return stack.hasTag() && stack.getTag().contains(ItemHelper.VIRTUAL_ITEM_ID) ? stack.getTag().getString(ItemHelper.VIRTUAL_ITEM_ID) : id;
}
}
10 changes: 7 additions & 3 deletions src/main/java/eu/pb4/polymer/resourcepack/DefaultRPBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -94,11 +94,15 @@ public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) th
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
Path fileOut;
try {
fileOut = output.resolve(assets.relativize(file));
fileOut = output.resolve(assets.relativize(file).toString());
} catch (Exception e) {
fileOut = file;
fileOut = output.resolve(file.toString());

}
fileOut.getParent().toFile().mkdirs();

try {
fileOut.getParent().toFile().mkdirs();
} catch (Exception e) { }

Files.copy(file, fileOut);
return FileVisitResult.CONTINUE;
Expand Down
5 changes: 5 additions & 0 deletions src/main/resources/polymer.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@
"block.AbstractBlockAccessor",
"block.AbstractBlockMixin",
"block.AbstractBlockSettingAccessor",
"block.BlockEventS2CPacketMixin",
"block.BlockPaletteMixin",
"block.BlockUpdateS2CPacketMixin",
"block.ChunkDataS2CPacketMixin",
"block.ChunkDeltaUpdateS2CPacketAccessor",
"block.ChunkDeltaUpdateS2CPacketMixin",
"block.FallingBlockEntityMixin",
"block.PlayerActionResponseS2CPacketMixin",
"block.ServerChunkManagerMixin",
"block.ServerPlayerInteractionManagerMixin",
"block.ServerPlayNetworkHandlerMixin",
Expand All @@ -28,6 +30,7 @@
"entity.LivingEntityMixin",
"entity.MobSpawnS2CPacketMixin",
"item.ItemStackContentMixin",
"item.ItemStackMixin",
"item.PacketByteBufMixin",
"item.ServerPlayNetworkHandlerMixin",
"other.CommandManagerMixin",
Expand All @@ -37,6 +40,8 @@
"polymc.BlockPolyGeneratorMixin"
],
"client": [
"item.ClickSlotC2SPacketMixin",
"item.CreativeInventoryActionC2SPacketMixin",
"item.EntityEquipmentUpdateS2CPacketMixin",
"item.InventoryS2CPacketMixin",
"item.ScreenHandlerSlotUpdateS2CPacketMixin"
Expand Down

0 comments on commit e303917

Please sign in to comment.