1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
|
/*
* spotless:off
* KubaTech - Gregtech Addon
* Copyright (C) 2022 - 2024 kuba6000
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this library. If not, see <https://www.gnu.org/licenses/>.
* spotless:on
*/
package kubatech.loaders.block.kubablock;
import static kubatech.kubatech.KT;
import java.lang.ref.WeakReference;
import java.util.HashMap;
import java.util.List;
import java.util.function.Function;
import net.minecraft.block.Block;
import net.minecraft.block.material.Material;
import net.minecraft.client.renderer.texture.IIconRegister;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.IIcon;
import net.minecraft.world.World;
import com.gtnewhorizons.modularui.api.screen.ITileWithModularUI;
import com.gtnewhorizons.modularui.api.screen.ModularUIContext;
import com.gtnewhorizons.modularui.api.screen.ModularWindow;
import com.gtnewhorizons.modularui.api.screen.UIBuildContext;
import com.gtnewhorizons.modularui.common.builder.UIBuilder;
import com.gtnewhorizons.modularui.common.builder.UIInfo;
import com.gtnewhorizons.modularui.common.internal.wrapper.ModularGui;
import com.gtnewhorizons.modularui.common.internal.wrapper.ModularUIContainer;
import kubatech.loaders.BlockLoader;
public class KubaBlock extends Block {
public static final Function<IModularUIContainerCreator, UIInfo<?, ?>> TileEntityUIFactory = containerConstructor -> UIBuilder
.of()
.container((player, world, x, y, z) -> {
TileEntity te = world.getTileEntity(x, y, z);
if (te instanceof ITileWithModularUI) {
final UIBuildContext buildContext = new UIBuildContext(player);
final ModularWindow window = ((ITileWithModularUI) te).createWindow(buildContext);
return containerConstructor
.createUIContainer(new ModularUIContext(buildContext, te::markDirty), window);
}
return null;
})
.gui(((player, world, x, y, z) -> {
if (!world.isRemote) return null;
TileEntity te = world.getTileEntity(x, y, z);
if (te instanceof ITileWithModularUI) {
final UIBuildContext buildContext = new UIBuildContext(player);
final ModularWindow window = ((ITileWithModularUI) te).createWindow(buildContext);
return new ModularGui(
containerConstructor.createUIContainer(new ModularUIContext(buildContext, null), window));
}
return null;
}))
.build();
public static final UIInfo<?, ?> defaultTileEntityUI = TileEntityUIFactory.apply(ModularUIContainer::new);
static final HashMap<Integer, BlockProxy> blocks = new HashMap<>();
private static int idCounter = 0;
public KubaBlock(Material p_i45394_1_) {
super(p_i45394_1_);
setCreativeTab(KT);
}
public ItemStack registerProxyBlock(BlockProxy block) {
blocks.put(idCounter, block);
block.itemInit(idCounter);
return new ItemStack(BlockLoader.kubaItemBlock, 1, idCounter++);
}
private BlockProxy getBlock(int id) {
return blocks.get(id);
}
WeakReference<World> lastAccessor = null;
int X, Y, Z;
public void setLastBlockAccess(World accessor, int x, int y, int z) {
lastAccessor = new WeakReference<>(accessor);
X = x;
Y = y;
Z = z;
}
@Override
public boolean hasTileEntity(int meta) {
return getBlock(meta) instanceof IProxyTileEntityProvider;
}
@SuppressWarnings({ "unchecked", "rawtypes" })
@Override
public void getSubBlocks(Item p_149666_1_, CreativeTabs p_149666_2_, List p_149666_3_) {
for (int i = 0; i < blocks.size(); i++) p_149666_3_.add(new ItemStack(p_149666_1_, 1, i));
}
@Override
public int damageDropped(int meta) {
return meta;
}
@Override
public void registerBlockIcons(IIconRegister p_149651_1_) {
blocks.values()
.forEach(b -> b.registerIcon(p_149651_1_));
}
@Override
public IIcon getIcon(int p_149691_1_, int p_149691_2_) {
return blocks.get(p_149691_2_)
.getIcon(p_149691_1_);
}
@Override
public String getLocalizedName() {
return "KUBABLOCK";
}
@Override
public TileEntity createTileEntity(World world, int metadata) {
if (!hasTileEntity(metadata)) return null;
return ((IProxyTileEntityProvider) getBlock(metadata)).createTileEntity(world);
}
@Override
public boolean onBlockActivated(World p_149727_1_, int p_149727_2_, int p_149727_3_, int p_149727_4_,
EntityPlayer p_149727_5_, int p_149727_6_, float p_149727_7_, float p_149727_8_, float p_149727_9_) {
return getBlock(p_149727_1_.getBlockMetadata(p_149727_2_, p_149727_3_, p_149727_4_))
.onActivated(p_149727_1_, p_149727_2_, p_149727_3_, p_149727_4_, p_149727_5_);
}
@Override
public void onBlockPlacedBy(World p_149689_1_, int p_149689_2_, int p_149689_3_, int p_149689_4_,
EntityLivingBase p_149689_5_, ItemStack p_149689_6_) {
getBlock(p_149689_6_.getItemDamage())
.onBlockPlaced(p_149689_1_, p_149689_2_, p_149689_3_, p_149689_4_, p_149689_5_, p_149689_6_);
}
@Override
public float getBlockHardness(World p_149712_1_, int p_149712_2_, int p_149712_3_, int p_149712_4_) {
return getBlock(p_149712_1_.getBlockMetadata(p_149712_2_, p_149712_3_, p_149712_4_)).getHardness();
}
@Override
public Material getMaterial() {
if (lastAccessor == null) return super.getMaterial();
World world = lastAccessor.get();
if (world == null) {
lastAccessor = null;
return super.getMaterial();
}
if (world.getBlock(X, Y, Z) != this) return super.getMaterial();
return getBlock(world.getBlockMetadata(X, Y, Z)).getMaterial();
}
@Override
public float getExplosionResistance(Entity par1Entity, World world, int x, int y, int z, double explosionX,
double explosionY, double explosionZ) {
return getBlock(world.getBlockMetadata(x, y, z)).getResistance();
}
@FunctionalInterface
public interface IModularUIContainerCreator {
ModularUIContainer createUIContainer(ModularUIContext context, ModularWindow mainWindow);
}
@FunctionalInterface
public interface IModularUIProvider {
UIInfo<?, ?> getUI();
}
}
|